Support Azure AD auth for Cosmos DB (#1104)
* Support Azure AD auth for Cosmos DB * Fixed linting errors * Tidying go.sum * Removed the need for nolint:shadow Co-authored-by: Simon Leet <31784195+CodeMonkeyLeet@users.noreply.github.com> Co-authored-by: Artur Souza <artursouza.ms@outlook.com> Co-authored-by: Dapr Bot <56698301+dapr-bot@users.noreply.github.com>
This commit is contained in:
parent
5e05c8d4ef
commit
a992cd19f0
|
|
@ -38,6 +38,9 @@ func NewEnvironmentSettings(resourceName string, values map[string]string) (Envi
|
|||
case "storage":
|
||||
// Azure Storage (data plane)
|
||||
es.Resource = azureEnv.ResourceIdentifiers.Storage
|
||||
case "cosmosdb":
|
||||
// Azure Cosmos DB (data plane)
|
||||
es.Resource = "https://" + azureEnv.CosmosDBDNSSuffix
|
||||
default:
|
||||
return es, errors.New("invalid resource name: " + resourceName)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/a8m/documentdb"
|
||||
"github.com/dapr/components-contrib/authentication/azure"
|
||||
"github.com/dapr/components-contrib/bindings"
|
||||
"github.com/dapr/kit/logger"
|
||||
)
|
||||
|
|
@ -46,11 +47,26 @@ func (c *CosmosDB) Init(metadata bindings.Metadata) error {
|
|||
}
|
||||
|
||||
c.partitionKey = m.PartitionKey
|
||||
client := documentdb.New(m.URL, &documentdb.Config{
|
||||
MasterKey: &documentdb.Key{
|
||||
|
||||
// Create the client; first, try authenticating with a master key, if present
|
||||
var config *documentdb.Config
|
||||
if m.MasterKey != "" {
|
||||
config = documentdb.NewConfig(&documentdb.Key{
|
||||
Key: m.MasterKey,
|
||||
},
|
||||
})
|
||||
})
|
||||
} else {
|
||||
// Fallback to using Azure AD
|
||||
env, errB := azure.NewEnvironmentSettings("cosmosdb", metadata.Properties)
|
||||
if errB != nil {
|
||||
return errB
|
||||
}
|
||||
spt, errB := env.GetServicePrincipalToken()
|
||||
if errB != nil {
|
||||
return errB
|
||||
}
|
||||
config = documentdb.NewConfigWithServicePrincipal(spt)
|
||||
}
|
||||
client := documentdb.New(m.URL, config)
|
||||
|
||||
dbs, err := client.QueryDatabases(&documentdb.Query{
|
||||
Query: "SELECT * FROM ROOT r WHERE r.id=@id",
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -20,7 +20,7 @@ require (
|
|||
github.com/DATA-DOG/go-sqlmock v1.5.0
|
||||
github.com/Shopify/sarama v1.23.1
|
||||
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
|
||||
github.com/a8m/documentdb v1.2.1-0.20190920062420-efdd52fe0905
|
||||
github.com/a8m/documentdb v1.3.0
|
||||
github.com/aerospike/aerospike-client-go v4.5.0+incompatible
|
||||
github.com/agrea/ptr v0.0.0-20180711073057-77a518d99b7b
|
||||
github.com/ajg/form v1.5.1 // indirect
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -131,8 +131,8 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrU
|
|||
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY=
|
||||
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
|
||||
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
|
||||
github.com/a8m/documentdb v1.2.1-0.20190920062420-efdd52fe0905 h1:lrOYmNobGcyWEjvMIMJERJx1Y4ttPFobY7RHAD+6e10=
|
||||
github.com/a8m/documentdb v1.2.1-0.20190920062420-efdd52fe0905/go.mod h1:4Z0mpi7fkyqjxUdGiNMO3vagyiUoiwLncaIX6AsW5z0=
|
||||
github.com/a8m/documentdb v1.3.0 h1:xzZQ6Ts02QesHeQdRr6doF7xfXYSsq9SUIlCqfJjbv4=
|
||||
github.com/a8m/documentdb v1.3.0/go.mod h1:4Z0mpi7fkyqjxUdGiNMO3vagyiUoiwLncaIX6AsW5z0=
|
||||
github.com/aerospike/aerospike-client-go v4.5.0+incompatible h1:6ALev/Ge4jW5avSLoqgvPYTh+FLeeDD9xDhzoMCNgOo=
|
||||
github.com/aerospike/aerospike-client-go v4.5.0+incompatible/go.mod h1:zj8LBEnWBDOVEIJt8LvaRvDG5ARAoa5dBeHaB472NRc=
|
||||
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import (
|
|||
"github.com/google/uuid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
"github.com/dapr/components-contrib/authentication/azure"
|
||||
"github.com/dapr/components-contrib/contenttype"
|
||||
"github.com/dapr/components-contrib/state"
|
||||
"github.com/dapr/kit/logger"
|
||||
|
|
@ -100,9 +101,6 @@ func (c *StateStore) Init(meta state.Metadata) error {
|
|||
if m.URL == "" {
|
||||
return errors.New("url is required")
|
||||
}
|
||||
if m.MasterKey == "" {
|
||||
return errors.New("masterKey is required")
|
||||
}
|
||||
if m.Database == "" {
|
||||
return errors.New("database is required")
|
||||
}
|
||||
|
|
@ -113,11 +111,25 @@ func (c *StateStore) Init(meta state.Metadata) error {
|
|||
return errors.New("contentType is required")
|
||||
}
|
||||
|
||||
client := documentdb.New(m.URL, &documentdb.Config{
|
||||
MasterKey: &documentdb.Key{
|
||||
// Create the client; first, try authenticating with a master key, if present
|
||||
var config *documentdb.Config
|
||||
if m.MasterKey != "" {
|
||||
config = documentdb.NewConfig(&documentdb.Key{
|
||||
Key: m.MasterKey,
|
||||
},
|
||||
})
|
||||
})
|
||||
} else {
|
||||
// Fallback to using Azure AD
|
||||
env, errB := azure.NewEnvironmentSettings("cosmosdb", meta.Properties)
|
||||
if errB != nil {
|
||||
return errB
|
||||
}
|
||||
spt, errB := env.GetServicePrincipalToken()
|
||||
if errB != nil {
|
||||
return errB
|
||||
}
|
||||
config = documentdb.NewConfigWithServicePrincipal(spt)
|
||||
}
|
||||
client := documentdb.New(m.URL, config)
|
||||
|
||||
dbs, err := client.QueryDatabases(&documentdb.Query{
|
||||
Query: "SELECT * FROM ROOT r WHERE r.id=@id",
|
||||
|
|
|
|||
Loading…
Reference in New Issue