Skip to content

Commit a992cd1

Browse files
ItalyPaleAleCodeMonkeyLeetartursouzadapr-bot
authored
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>
1 parent 5e05c8d commit a992cd1

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

authentication/azure/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func NewEnvironmentSettings(resourceName string, values map[string]string) (Envi
3838
case "storage":
3939
// Azure Storage (data plane)
4040
es.Resource = azureEnv.ResourceIdentifiers.Storage
41+
case "cosmosdb":
42+
// Azure Cosmos DB (data plane)
43+
es.Resource = "https://" + azureEnv.CosmosDBDNSSuffix
4144
default:
4245
return es, errors.New("invalid resource name: " + resourceName)
4346
}

bindings/azure/cosmosdb/cosmosdb.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/a8m/documentdb"
14+
"github.com/dapr/components-contrib/authentication/azure"
1415
"github.com/dapr/components-contrib/bindings"
1516
"github.com/dapr/kit/logger"
1617
)
@@ -46,11 +47,26 @@ func (c *CosmosDB) Init(metadata bindings.Metadata) error {
4647
}
4748

4849
c.partitionKey = m.PartitionKey
49-
client := documentdb.New(m.URL, &documentdb.Config{
50-
MasterKey: &documentdb.Key{
50+
51+
// Create the client; first, try authenticating with a master key, if present
52+
var config *documentdb.Config
53+
if m.MasterKey != "" {
54+
config = documentdb.NewConfig(&documentdb.Key{
5155
Key: m.MasterKey,
52-
},
53-
})
56+
})
57+
} else {
58+
// Fallback to using Azure AD
59+
env, errB := azure.NewEnvironmentSettings("cosmosdb", metadata.Properties)
60+
if errB != nil {
61+
return errB
62+
}
63+
spt, errB := env.GetServicePrincipalToken()
64+
if errB != nil {
65+
return errB
66+
}
67+
config = documentdb.NewConfigWithServicePrincipal(spt)
68+
}
69+
client := documentdb.New(m.URL, config)
5470

5571
dbs, err := client.QueryDatabases(&documentdb.Query{
5672
Query: "SELECT * FROM ROOT r WHERE r.id=@id",

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/DATA-DOG/go-sqlmock v1.5.0
2121
github.com/Shopify/sarama v1.23.1
2222
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
23-
github.com/a8m/documentdb v1.2.1-0.20190920062420-efdd52fe0905
23+
github.com/a8m/documentdb v1.3.0
2424
github.com/aerospike/aerospike-client-go v4.5.0+incompatible
2525
github.com/agrea/ptr v0.0.0-20180711073057-77a518d99b7b
2626
github.com/ajg/form v1.5.1 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrU
131131
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 h1:5sXbqlSomvdjlRbWyNqkPsJ3Fg+tQZCbgeX1VGljbQY=
132132
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
133133
github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g=
134-
github.com/a8m/documentdb v1.2.1-0.20190920062420-efdd52fe0905 h1:lrOYmNobGcyWEjvMIMJERJx1Y4ttPFobY7RHAD+6e10=
135-
github.com/a8m/documentdb v1.2.1-0.20190920062420-efdd52fe0905/go.mod h1:4Z0mpi7fkyqjxUdGiNMO3vagyiUoiwLncaIX6AsW5z0=
134+
github.com/a8m/documentdb v1.3.0 h1:xzZQ6Ts02QesHeQdRr6doF7xfXYSsq9SUIlCqfJjbv4=
135+
github.com/a8m/documentdb v1.3.0/go.mod h1:4Z0mpi7fkyqjxUdGiNMO3vagyiUoiwLncaIX6AsW5z0=
136136
github.com/aerospike/aerospike-client-go v4.5.0+incompatible h1:6ALev/Ge4jW5avSLoqgvPYTh+FLeeDD9xDhzoMCNgOo=
137137
github.com/aerospike/aerospike-client-go v4.5.0+incompatible/go.mod h1:zj8LBEnWBDOVEIJt8LvaRvDG5ARAoa5dBeHaB472NRc=
138138
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=

state/azure/cosmosdb/cosmosdb.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/google/uuid"
1919
jsoniter "github.com/json-iterator/go"
2020

21+
"github.com/dapr/components-contrib/authentication/azure"
2122
"github.com/dapr/components-contrib/contenttype"
2223
"github.com/dapr/components-contrib/state"
2324
"github.com/dapr/kit/logger"
@@ -100,9 +101,6 @@ func (c *StateStore) Init(meta state.Metadata) error {
100101
if m.URL == "" {
101102
return errors.New("url is required")
102103
}
103-
if m.MasterKey == "" {
104-
return errors.New("masterKey is required")
105-
}
106104
if m.Database == "" {
107105
return errors.New("database is required")
108106
}
@@ -113,11 +111,25 @@ func (c *StateStore) Init(meta state.Metadata) error {
113111
return errors.New("contentType is required")
114112
}
115113

116-
client := documentdb.New(m.URL, &documentdb.Config{
117-
MasterKey: &documentdb.Key{
114+
// Create the client; first, try authenticating with a master key, if present
115+
var config *documentdb.Config
116+
if m.MasterKey != "" {
117+
config = documentdb.NewConfig(&documentdb.Key{
118118
Key: m.MasterKey,
119-
},
120-
})
119+
})
120+
} else {
121+
// Fallback to using Azure AD
122+
env, errB := azure.NewEnvironmentSettings("cosmosdb", meta.Properties)
123+
if errB != nil {
124+
return errB
125+
}
126+
spt, errB := env.GetServicePrincipalToken()
127+
if errB != nil {
128+
return errB
129+
}
130+
config = documentdb.NewConfigWithServicePrincipal(spt)
131+
}
132+
client := documentdb.New(m.URL, config)
121133

122134
dbs, err := client.QueryDatabases(&documentdb.Query{
123135
Query: "SELECT * FROM ROOT r WHERE r.id=@id",

0 commit comments

Comments
 (0)