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.
Add KeyCredential and SASCredential types (Azure#21553)
* Add KeyCredential and SASCredential types Includes supporting pipeline policies, config options, etc. * add tests * change Format func to Prefix string * remove error return value from constructors * remove error return for Update method
- Loading branch information
1 parent
e5225ff
commit 0e74539
Showing
9 changed files
with
307 additions
and
0 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
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,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/exported" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
) | ||
|
||
// KeyCredentialPolicy authorizes requests with a [azcore.KeyCredential]. | ||
type KeyCredentialPolicy struct { | ||
cred *exported.KeyCredential | ||
header string | ||
prefix string | ||
} | ||
|
||
// KeyCredentialPolicyOptions contains the optional values configuring [KeyCredentialPolicy]. | ||
type KeyCredentialPolicyOptions struct { | ||
// Prefix is used if the key requires a prefix before it's inserted into the HTTP request. | ||
Prefix string | ||
} | ||
|
||
// NewKeyCredentialPolicy creates a new instance of [KeyCredentialPolicy]. | ||
// - cred is the [azcore.KeyCredential] used to authenticate with the service | ||
// - header is the name of the HTTP request header in which the key is placed | ||
// - options contains optional configuration, pass nil to accept the default values | ||
func NewKeyCredentialPolicy(cred *exported.KeyCredential, header string, options *KeyCredentialPolicyOptions) *KeyCredentialPolicy { | ||
if options == nil { | ||
options = &KeyCredentialPolicyOptions{} | ||
} | ||
return &KeyCredentialPolicy{ | ||
cred: cred, | ||
header: header, | ||
prefix: options.Prefix, | ||
} | ||
} | ||
|
||
// Do implementes the Do method on the [policy.Polilcy] interface. | ||
func (k *KeyCredentialPolicy) Do(req *policy.Request) (*http.Response, error) { | ||
val := exported.KeyCredentialGet(k.cred) | ||
if k.prefix != "" { | ||
val = k.prefix + val | ||
} | ||
req.Raw().Header.Add(k.header, val) | ||
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,50 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/exported" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKeyCredentialPolicy(t *testing.T) { | ||
const key = "foo" | ||
cred := exported.NewKeyCredential(key) | ||
|
||
const headerName = "fake-auth" | ||
policy := NewKeyCredentialPolicy(cred, headerName, nil) | ||
require.NotNil(t, policy) | ||
|
||
pl := exported.NewPipeline(shared.TransportFunc(func(req *http.Request) (*http.Response, error) { | ||
require.EqualValues(t, key, req.Header.Get(headerName)) | ||
return &http.Response{}, nil | ||
}), policy) | ||
|
||
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com") | ||
require.NoError(t, err) | ||
|
||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
|
||
policy = NewKeyCredentialPolicy(cred, headerName, &KeyCredentialPolicyOptions{ | ||
Prefix: "Prefix: ", | ||
}) | ||
require.NotNil(t, policy) | ||
|
||
pl = exported.NewPipeline(shared.TransportFunc(func(req *http.Request) (*http.Response, error) { | ||
require.EqualValues(t, "Prefix: "+key, req.Header.Get(headerName)) | ||
return &http.Response{}, nil | ||
}), policy) | ||
|
||
req, err = NewRequest(context.Background(), http.MethodGet, "http://contoso.com") | ||
require.NoError(t, err) | ||
|
||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
} |
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,39 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/exported" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
) | ||
|
||
// SASCredentialPolicy authorizes requests with a [azcore.SASCredential]. | ||
type SASCredentialPolicy struct { | ||
cred *exported.SASCredential | ||
header string | ||
} | ||
|
||
// SASCredentialPolicyOptions contains the optional values configuring [SASCredentialPolicy]. | ||
type SASCredentialPolicyOptions struct { | ||
// placeholder for future optional values | ||
} | ||
|
||
// NewSASCredentialPolicy creates a new instance of [SASCredentialPolicy]. | ||
// - cred is the [azcore.SASCredential] used to authenticate with the service | ||
// - header is the name of the HTTP request header in which the shared access signature is placed | ||
// - options contains optional configuration, pass nil to accept the default values | ||
func NewSASCredentialPolicy(cred *exported.SASCredential, header string, options *SASCredentialPolicyOptions) *SASCredentialPolicy { | ||
return &SASCredentialPolicy{ | ||
cred: cred, | ||
header: header, | ||
} | ||
} | ||
|
||
// Do implementes the Do method on the [policy.Polilcy] interface. | ||
func (k *SASCredentialPolicy) Do(req *policy.Request) (*http.Response, error) { | ||
req.Raw().Header.Add(k.header, exported.SASCredentialGet(k.cred)) | ||
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,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/exported" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSASCredentialPolicy(t *testing.T) { | ||
const key = "foo" | ||
cred := exported.NewSASCredential(key) | ||
|
||
const headerName = "fake-auth" | ||
policy := NewSASCredentialPolicy(cred, headerName, nil) | ||
require.NotNil(t, policy) | ||
|
||
pl := exported.NewPipeline(shared.TransportFunc(func(req *http.Request) (*http.Response, error) { | ||
require.EqualValues(t, key, req.Header.Get(headerName)) | ||
return &http.Response{}, nil | ||
}), policy) | ||
|
||
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com") | ||
require.NoError(t, err) | ||
|
||
_, err = pl.Do(req) | ||
require.NoError(t, err) | ||
} |