Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding UDK/UDC to service client (#19141)
* Adding UDK/UDC to service client * Adding GetUDKSASURL * Removing changes as requested * Additional changes to remove * Addressing comments, cleaning up code, adding wip test * Modifications to test * Clean up * Addressing comments in PR * Adding helper methods to only allow SDK calls * Adding to example_test and making udc immutable * Adding helper method for GetAccountName * Additional clean up * Minor changes & commenting out test * Renaming of opts * Changing option name for GetUserDelegationCredential * Minor type renaming * Putting GetUserDelegationCredential on service.client * Adding doc comment * Updating doc comment for KeyInfo * Fixing example test with handle error * Fixing GetUserDelegationCredential and examples - Service.Client * Removing NewUserDelegationCredential from models.go
- Loading branch information
1 parent
425f9f6
commit 6ba57ce
Showing
12 changed files
with
426 additions
and
40 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
64 changes: 64 additions & 0 deletions
64
sdk/storage/azblob/internal/exported/user_delegation_credential.go
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,64 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package exported | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/generated" | ||
) | ||
|
||
// NewUserDelegationCredential creates a new UserDelegationCredential using a Storage account's Name and a user delegation Key from it | ||
func NewUserDelegationCredential(accountName string, udk UserDelegationKey) *UserDelegationCredential { | ||
return &UserDelegationCredential{ | ||
accountName: accountName, | ||
userDelegationKey: udk, | ||
} | ||
} | ||
|
||
// UserDelegationKey contains UserDelegationKey. | ||
type UserDelegationKey = generated.UserDelegationKey | ||
|
||
// UserDelegationCredential contains an account's name and its user delegation key. | ||
type UserDelegationCredential struct { | ||
accountName string | ||
userDelegationKey UserDelegationKey | ||
} | ||
|
||
// AccountName returns the Storage account's Name | ||
func (f *UserDelegationCredential) getAccountName() string { | ||
return f.accountName | ||
} | ||
|
||
// GetUDKParams is a helper method for accessing the user delegation key parameters outside of this package. | ||
func GetAccountName(udc *UserDelegationCredential) string { | ||
return udc.getAccountName() | ||
} | ||
|
||
// computeHMACSHA256 generates a hash signature for an HTTP request or for a SAS. | ||
func (f *UserDelegationCredential) computeHMACSHA256(message string) (string, error) { | ||
bytes, _ := base64.StdEncoding.DecodeString(*f.userDelegationKey.Value) | ||
h := hmac.New(sha256.New, bytes) | ||
_, err := h.Write([]byte(message)) | ||
return base64.StdEncoding.EncodeToString(h.Sum(nil)), err | ||
} | ||
|
||
// ComputeUDCHMACSHA256 is a helper method for computing the signed string outside of this package. | ||
func ComputeUDCHMACSHA256(udc *UserDelegationCredential, message string) (string, error) { | ||
return udc.computeHMACSHA256(message) | ||
} | ||
|
||
// GetUDKParams returns UserDelegationKey | ||
func (f *UserDelegationCredential) getUDKParams() *UserDelegationKey { | ||
return &f.userDelegationKey | ||
} | ||
|
||
// GetUDKParams is a helper method for accessing the user delegation key parameters outside of this package. | ||
func GetUDKParams(udc *UserDelegationCredential) *UserDelegationKey { | ||
return udc.getUDKParams() | ||
} |
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
Oops, something went wrong.