forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cosmos DB: Add AAD authentication (Azure#17742)
Adding azcosmos.NewClient with support for TokenCredential
- Loading branch information
Showing
12 changed files
with
413 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package azcosmos | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
) | ||
|
||
const lenBearerTokenPrefix = len("Bearer ") | ||
|
||
type cosmosBearerTokenPolicy struct { | ||
} | ||
|
||
func (b *cosmosBearerTokenPolicy) Do(req *policy.Request) (*http.Response, error) { | ||
currentAuthorization := req.Raw().Header.Get(headerAuthorization) | ||
if currentAuthorization == "" { | ||
return nil, errors.New("authorization header is missing") | ||
} | ||
|
||
token := currentAuthorization[lenBearerTokenPrefix:] | ||
req.Raw().Header.Set(headerAuthorization, fmt.Sprintf("type=aad&ver=1.0&sig=%v", token)) | ||
return req.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package azcosmos | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
azruntime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" | ||
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock" | ||
) | ||
|
||
func TestConvertBearerToken(t *testing.T) { | ||
srv, close := mock.NewTLSServer() | ||
defer close() | ||
srv.SetResponse(mock.WithStatusCode(http.StatusOK)) | ||
|
||
verifier := bearerTokenVerify{} | ||
pl := azruntime.NewPipeline("azcosmostest", "v1.0.0", azruntime.PipelineOptions{PerCall: []policy.Policy{&mockAuthPolicy{}, &cosmosBearerTokenPolicy{}, &verifier}}, &policy.ClientOptions{Transport: srv}) | ||
req, err := azruntime.NewRequest(context.Background(), http.MethodGet, srv.URL()) | ||
req.SetOperationValue(pipelineRequestOptions{ | ||
isWriteOperation: true, | ||
}) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
_, err = pl.Do(req) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if verifier.authHeaderContent != "type=aad&ver=1.0&sig=this is a test token" { | ||
t.Fatalf("Expected auth header content to be 'type=aad&ver=1.0&sig=this is a test token', got %s", verifier.authHeaderContent) | ||
} | ||
} | ||
|
||
type bearerTokenVerify struct { | ||
authHeaderContent string | ||
} | ||
|
||
func (p *bearerTokenVerify) Do(req *policy.Request) (*http.Response, error) { | ||
p.authHeaderContent = req.Raw().Header.Get(headerAuthorization) | ||
|
||
return req.Next() | ||
} | ||
|
||
type mockAuthPolicy struct{} | ||
|
||
func (p *mockAuthPolicy) Do(req *policy.Request) (*http.Response, error) { | ||
req.Raw().Header.Set(headerAuthorization, "Bearer this is a test token") | ||
|
||
return req.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package azcosmos | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestAAD(t *testing.T) { | ||
emulatorTests := newEmulatorTests(t) | ||
client := emulatorTests.getClient(t) | ||
|
||
database := emulatorTests.createDatabase(t, context.TODO(), client, "aadTest") | ||
defer emulatorTests.deleteDatabase(t, context.TODO(), database) | ||
properties := ContainerProperties{ | ||
ID: "aContainer", | ||
PartitionKeyDefinition: PartitionKeyDefinition{ | ||
Paths: []string{"/id"}, | ||
}, | ||
} | ||
|
||
_, err := database.CreateContainer(context.TODO(), properties, nil) | ||
if err != nil { | ||
t.Fatalf("Failed to create container: %v", err) | ||
} | ||
|
||
aadClient := emulatorTests.getAadClient(t) | ||
|
||
item := map[string]string{ | ||
"id": "1", | ||
"value": "2", | ||
} | ||
|
||
container, _ := aadClient.NewContainer("aadTest", "aContainer") | ||
pk := NewPartitionKeyString("1") | ||
|
||
marshalled, err := json.Marshal(item) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
itemResponse, err := container.CreateItem(context.TODO(), pk, marshalled, nil) | ||
if err != nil { | ||
t.Fatalf("Failed to create item: %v", err) | ||
} | ||
|
||
if itemResponse.SessionToken == "" { | ||
t.Fatalf("Session token is empty") | ||
} | ||
|
||
// No content on write by default | ||
if len(itemResponse.Value) != 0 { | ||
t.Fatalf("Expected empty response, got %v", itemResponse.Value) | ||
} | ||
|
||
itemResponse, err = container.ReadItem(context.TODO(), pk, "1", nil) | ||
if err != nil { | ||
t.Fatalf("Failed to read item: %v", err) | ||
} | ||
|
||
if len(itemResponse.Value) == 0 { | ||
t.Fatalf("Expected non-empty response, got %v", itemResponse.Value) | ||
} | ||
|
||
var itemResponseBody map[string]interface{} | ||
err = json.Unmarshal(itemResponse.Value, &itemResponseBody) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal item response: %v", err) | ||
} | ||
if itemResponseBody["id"] != "1" { | ||
t.Fatalf("Expected id to be 1, got %v", itemResponseBody["id"]) | ||
} | ||
if itemResponseBody["value"] != "2" { | ||
t.Fatalf("Expected value to be 2, got %v", itemResponseBody["value"]) | ||
} | ||
|
||
item["value"] = "3" | ||
marshalled, err = json.Marshal(item) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
itemResponse, err = container.ReplaceItem(context.TODO(), pk, "1", marshalled, &ItemOptions{EnableContentResponseOnWrite: true}) | ||
if err != nil { | ||
t.Fatalf("Failed to replace item: %v", err) | ||
} | ||
|
||
// Explicitly requesting body on write | ||
if len(itemResponse.Value) == 0 { | ||
t.Fatalf("Expected non-empty response, got %v", itemResponse.Value) | ||
} | ||
|
||
err = json.Unmarshal(itemResponse.Value, &itemResponseBody) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal item response: %v", err) | ||
} | ||
if itemResponseBody["id"] != "1" { | ||
t.Fatalf("Expected id to be 1, got %v", itemResponseBody["id"]) | ||
} | ||
if itemResponseBody["value"] != "3" { | ||
t.Fatalf("Expected value to be 3, got %v", itemResponseBody["value"]) | ||
} | ||
|
||
item["value"] = "4" | ||
marshalled, err = json.Marshal(item) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
itemResponse, err = container.UpsertItem(context.TODO(), pk, marshalled, &ItemOptions{EnableContentResponseOnWrite: true}) | ||
if err != nil { | ||
t.Fatalf("Failed to upsert item: %v", err) | ||
} | ||
|
||
// Explicitly requesting body on write | ||
if len(itemResponse.Value) == 0 { | ||
t.Fatalf("Expected non-empty response, got %v", itemResponse.Value) | ||
} | ||
|
||
err = json.Unmarshal(itemResponse.Value, &itemResponseBody) | ||
if err != nil { | ||
t.Fatalf("Failed to unmarshal item response: %v", err) | ||
} | ||
if itemResponseBody["id"] != "1" { | ||
t.Fatalf("Expected id to be 1, got %v", itemResponseBody["id"]) | ||
} | ||
if itemResponseBody["value"] != "4" { | ||
t.Fatalf("Expected value to be 4, got %v", itemResponseBody["value"]) | ||
} | ||
|
||
itemResponse, err = container.DeleteItem(context.TODO(), pk, "1", nil) | ||
if err != nil { | ||
t.Fatalf("Failed to replace item: %v", err) | ||
} | ||
|
||
if len(itemResponse.Value) != 0 { | ||
t.Fatalf("Expected empty response, got %v", itemResponse.Value) | ||
} | ||
} |
Oops, something went wrong.