-
Notifications
You must be signed in to change notification settings - Fork 973
azfile: SAS #20054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
azfile: SAS #20054
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
9fba771
Init
souravgupta-msft 62e45d0
Reformat code
souravgupta-msft 0493788
Adding transforms
souravgupta-msft 9ddaf65
Adding transforms
souravgupta-msft 023f75a
Adding transforms
souravgupta-msft 51235e4
Typo
souravgupta-msft 340204f
Updating go version
souravgupta-msft 264812d
Converting to time.Time
souravgupta-msft 8967e3a
Adding service client
souravgupta-msft b6f6c3f
Updating autorest version
souravgupta-msft 01c699d
Adding changelog and build.go files
souravgupta-msft c1a8c31
Merge branch 'sourav/auto-generation' of https://github.com/Azure/azu…
souravgupta-msft e111ae0
Adding methods in share client
souravgupta-msft 5ad7696
Adding lease methods in share client
souravgupta-msft fb5c2c5
Pulling from feature branch
souravgupta-msft 09adce6
changes in lease share methods
souravgupta-msft cc31841
Adding methods for directory client
souravgupta-msft dfa3679
Adding methods for file client
souravgupta-msft 70a31a5
File client methods
souravgupta-msft 0e12299
Format checks
souravgupta-msft e2c970c
Format checks
souravgupta-msft b660ce0
Changing type of Etag from *string to *azcore.Etag
souravgupta-msft 2cf6002
Capitalise NTFS and SMB fields
souravgupta-msft b22ae02
Adding download methods same as azblob
souravgupta-msft 7f715dd
Adding lease client
souravgupta-msft de69fe8
Capitalise SMB
souravgupta-msft d557aa3
Renaming the lease methods
souravgupta-msft 0b73838
autorest transform - remove item suffix
souravgupta-msft 65d93fd
Renaming FileID to ID and capitalising cors
souravgupta-msft ee4e518
adding internal package code
souravgupta-msft 734fe0e
Review comments
souravgupta-msft 604da07
Merge branch 'sourav/serviceClient' of https://github.com/Azure/azure…
souravgupta-msft 6f891f4
Service properties rename
souravgupta-msft ed2407b
Merge branch 'sourav/serviceClient' of https://github.com/Azure/azure…
souravgupta-msft f4b2d3b
Adding list and set properties
souravgupta-msft 88b392f
service client tests
souravgupta-msft a454119
Merge branch 'main' of https://github.com/Azure/azure-sdk-for-go into…
souravgupta-msft de3a239
Merge branch 'feature/azfile' of https://github.com/Azure/azure-sdk-f…
souravgupta-msft 706b503
Merge branch 'sourav/serviceClient' of https://github.com/Azure/azure…
souravgupta-msft d997002
Adding error codes
souravgupta-msft da163c1
Merging from feature branch
souravgupta-msft 226ef3b
removing container error code
souravgupta-msft 58a7389
Removing client creation using OAuth
souravgupta-msft 84d3a03
Adding assets.json
souravgupta-msft 13a982c
Adding few recordings
souravgupta-msft d9edf3a
Few tests to unrecorded
souravgupta-msft 3f62bc7
account and service SAS
souravgupta-msft 0f917c8
GetSASURL() and url_parts.go
souravgupta-msft a33d051
Adding sas based tests
souravgupta-msft a387962
small change
souravgupta-msft 28cadaf
Merging from feature branch
souravgupta-msft 4b4326e
use directory and file path
souravgupta-msft 4eadd55
Adding resource type parsing
souravgupta-msft b472d97
Review comments
souravgupta-msft 2502a50
Few changes
souravgupta-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
| //go:build go1.18 | ||
| // +build go1.18 | ||
|
|
||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| package sas | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/internal/exported" | ||
| ) | ||
|
|
||
| // SharedKeyCredential contains an account's name and its primary or secondary key. | ||
| type SharedKeyCredential = exported.SharedKeyCredential | ||
|
|
||
| // AccountSignatureValues is used to generate a Shared Access Signature (SAS) for an Azure Storage account. | ||
| // For more information, see https://docs.microsoft.com/rest/api/storageservices/constructing-an-account-sas | ||
| type AccountSignatureValues struct { | ||
| Version string `param:"sv"` // If not specified, this format to SASVersion | ||
| Protocol Protocol `param:"spr"` // See the SASProtocol* constants | ||
| StartTime time.Time `param:"st"` // Not specified if IsZero | ||
| ExpiryTime time.Time `param:"se"` // Not specified if IsZero | ||
| Permissions string `param:"sp"` // Create by initializing a AccountSASPermissions and then call String() | ||
| IPRange IPRange `param:"sip"` | ||
| ResourceTypes string `param:"srt"` // Create by initializing AccountSASResourceTypes and then call String() | ||
| } | ||
|
|
||
| // SignWithSharedKey uses an account's shared key credential to sign this signature values to produce | ||
| // the proper SAS query parameters. | ||
| func (v AccountSignatureValues) SignWithSharedKey(sharedKeyCredential *SharedKeyCredential) (QueryParameters, error) { | ||
| // https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS | ||
| if v.ExpiryTime.IsZero() || v.Permissions == "" || v.ResourceTypes == "" { | ||
| return QueryParameters{}, errors.New("account SAS is missing at least one of these: ExpiryTime, Permissions, Service, or ResourceType") | ||
| } | ||
| if v.Version == "" { | ||
| v.Version = Version | ||
| } | ||
| perms, err := parseAccountPermissions(v.Permissions) | ||
| if err != nil { | ||
| return QueryParameters{}, err | ||
| } | ||
| v.Permissions = perms.String() | ||
|
|
||
| resources, err := parseAccountResourceTypes(v.ResourceTypes) | ||
| if err != nil { | ||
| return QueryParameters{}, err | ||
| } | ||
| v.ResourceTypes = resources.String() | ||
|
|
||
| startTime, expiryTime, _ := formatTimesForSigning(v.StartTime, v.ExpiryTime, time.Time{}) | ||
|
|
||
| stringToSign := strings.Join([]string{ | ||
| sharedKeyCredential.AccountName(), | ||
| v.Permissions, | ||
| "f", // file service | ||
| v.ResourceTypes, | ||
| startTime, | ||
| expiryTime, | ||
| v.IPRange.String(), | ||
| string(v.Protocol), | ||
| v.Version, | ||
| ""}, // That is right, the account SAS requires a terminating extra newline | ||
| "\n") | ||
|
|
||
| signature, err := exported.ComputeHMACSHA256(sharedKeyCredential, stringToSign) | ||
| if err != nil { | ||
| return QueryParameters{}, err | ||
| } | ||
| p := QueryParameters{ | ||
| // Common SAS parameters | ||
| version: v.Version, | ||
| protocol: v.Protocol, | ||
| startTime: v.StartTime, | ||
| expiryTime: v.ExpiryTime, | ||
| permissions: v.Permissions, | ||
| ipRange: v.IPRange, | ||
|
|
||
| // Account-specific SAS parameters | ||
| services: "f", // will always be "f" for Azure File | ||
| resourceTypes: v.ResourceTypes, | ||
|
|
||
| // Calculated SAS signature | ||
| signature: signature, | ||
| } | ||
|
|
||
| return p, nil | ||
| } | ||
|
|
||
| // AccountPermissions type simplifies creating the permissions string for an Azure Storage Account SAS. | ||
| // Initialize an instance of this type and then call its String method to set AccountSASSignature value's Permissions field. | ||
| type AccountPermissions struct { | ||
| Read, Write, Delete, List, Create bool | ||
| } | ||
|
|
||
| // String produces the SAS permissions string for an Azure Storage account. | ||
| // Call this method to set AccountSignatureValues' Permissions field. | ||
| func (p *AccountPermissions) String() string { | ||
| var buffer bytes.Buffer | ||
| if p.Read { | ||
| buffer.WriteRune('r') | ||
| } | ||
| if p.Write { | ||
| buffer.WriteRune('w') | ||
| } | ||
| if p.Delete { | ||
| buffer.WriteRune('d') | ||
| } | ||
| if p.List { | ||
| buffer.WriteRune('l') | ||
| } | ||
| if p.Create { | ||
| buffer.WriteRune('c') | ||
| } | ||
| return buffer.String() | ||
| } | ||
|
|
||
| // parseAccountPermissions initializes the AccountSASPermissions' fields from a string. | ||
| func parseAccountPermissions(s string) (AccountPermissions, error) { | ||
| p := AccountPermissions{} // Clear out the flags | ||
| for _, r := range s { | ||
| switch r { | ||
| case 'r': | ||
| p.Read = true | ||
| case 'w': | ||
| p.Write = true | ||
| case 'd': | ||
| p.Delete = true | ||
| case 'l': | ||
| p.List = true | ||
| case 'c': | ||
| p.Create = true | ||
| default: | ||
| return AccountPermissions{}, fmt.Errorf("invalid permission character: '%v'", r) | ||
| } | ||
| } | ||
| return p, nil | ||
| } | ||
|
|
||
| // AccountResourceTypes type simplifies creating the resource types string for an Azure Storage Account SAS. | ||
| // Initialize an instance of this type and then call its String method to set AccountSignatureValues' ResourceTypes field. | ||
| type AccountResourceTypes struct { | ||
| Service, Container, Object bool | ||
| } | ||
|
|
||
| // String produces the SAS resource types string for an Azure Storage account. | ||
| // Call this method to set AccountSignatureValues' ResourceTypes field. | ||
| func (rt *AccountResourceTypes) String() string { | ||
| var buffer bytes.Buffer | ||
| if rt.Service { | ||
| buffer.WriteRune('s') | ||
| } | ||
| if rt.Container { | ||
| buffer.WriteRune('c') | ||
| } | ||
| if rt.Object { | ||
| buffer.WriteRune('o') | ||
| } | ||
| return buffer.String() | ||
| } | ||
|
|
||
| // parseAccountResourceTypes initializes the AccountResourceTypes' fields from a string. | ||
| func parseAccountResourceTypes(s string) (AccountResourceTypes, error) { | ||
| rt := AccountResourceTypes{} | ||
| for _, r := range s { | ||
| switch r { | ||
| case 's': | ||
| rt.Service = true | ||
| case 'c': | ||
| rt.Container = true | ||
| case 'o': | ||
| rt.Object = true | ||
| default: | ||
| return AccountResourceTypes{}, fmt.Errorf("invalid resource type character: '%v'", r) | ||
| } | ||
| } | ||
| return rt, nil | ||
| } | ||
This file contains hidden or 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,124 @@ | ||
| //go:build go1.18 | ||
| // +build go1.18 | ||
|
|
||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| package sas | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/require" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestAccountPermissions_String(t *testing.T) { | ||
| testdata := []struct { | ||
| input AccountPermissions | ||
| expected string | ||
| }{ | ||
| {input: AccountPermissions{Read: true}, expected: "r"}, | ||
| {input: AccountPermissions{Write: true}, expected: "w"}, | ||
| {input: AccountPermissions{Delete: true}, expected: "d"}, | ||
| {input: AccountPermissions{List: true}, expected: "l"}, | ||
| {input: AccountPermissions{Create: true}, expected: "c"}, | ||
| {input: AccountPermissions{ | ||
| Read: true, | ||
| Write: true, | ||
| Delete: true, | ||
| List: true, | ||
| Create: true, | ||
| }, expected: "rwdlc"}, | ||
| } | ||
| for _, c := range testdata { | ||
| require.Equal(t, c.expected, c.input.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestAccountPermissions_Parse(t *testing.T) { | ||
| testdata := []struct { | ||
| input string | ||
| expected AccountPermissions | ||
| }{ | ||
| {expected: AccountPermissions{Read: true}, input: "r"}, | ||
| {expected: AccountPermissions{Write: true}, input: "w"}, | ||
| {expected: AccountPermissions{Delete: true}, input: "d"}, | ||
| {expected: AccountPermissions{List: true}, input: "l"}, | ||
| {expected: AccountPermissions{Create: true}, input: "c"}, | ||
| {expected: AccountPermissions{ | ||
| Read: true, | ||
| Write: true, | ||
| Delete: true, | ||
| List: true, | ||
| Create: true, | ||
| }, input: "rwdlc"}, | ||
| {expected: AccountPermissions{ | ||
| Read: true, | ||
| Write: true, | ||
| Delete: true, | ||
| List: true, | ||
| Create: true, | ||
| }, input: "rcdlw"}, | ||
| } | ||
| for _, c := range testdata { | ||
| permissions, err := parseAccountPermissions(c.input) | ||
| require.Nil(t, err) | ||
| require.Equal(t, c.expected, permissions) | ||
| } | ||
| } | ||
|
|
||
| func TestAccountPermissions_ParseNegative(t *testing.T) { | ||
| _, err := parseAccountPermissions("rwldcz") // Here 'z' is invalid | ||
| require.NotNil(t, err) | ||
| require.Contains(t, err.Error(), "122") | ||
| } | ||
|
|
||
| func TestAccountResourceTypes_String(t *testing.T) { | ||
| testdata := []struct { | ||
| input AccountResourceTypes | ||
| expected string | ||
| }{ | ||
| {input: AccountResourceTypes{Service: true}, expected: "s"}, | ||
| {input: AccountResourceTypes{Container: true}, expected: "c"}, | ||
| {input: AccountResourceTypes{Object: true}, expected: "o"}, | ||
| {input: AccountResourceTypes{ | ||
| Service: true, | ||
| Container: true, | ||
| Object: true, | ||
| }, expected: "sco"}, | ||
| } | ||
| for _, c := range testdata { | ||
| require.Equal(t, c.expected, c.input.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestAccountResourceTypes_Parse(t *testing.T) { | ||
| testdata := []struct { | ||
| input string | ||
| expected AccountResourceTypes | ||
| }{ | ||
| {expected: AccountResourceTypes{Service: true}, input: "s"}, | ||
| {expected: AccountResourceTypes{Container: true}, input: "c"}, | ||
| {expected: AccountResourceTypes{Object: true}, input: "o"}, | ||
| {expected: AccountResourceTypes{ | ||
| Service: true, | ||
| Container: true, | ||
| Object: true, | ||
| }, input: "sco"}, | ||
| {expected: AccountResourceTypes{ | ||
| Service: true, | ||
| Container: true, | ||
| Object: true, | ||
| }, input: "osc"}, | ||
| } | ||
| for _, c := range testdata { | ||
| permissions, err := parseAccountResourceTypes(c.input) | ||
| require.Nil(t, err) | ||
| require.Equal(t, c.expected, permissions) | ||
| } | ||
| } | ||
|
|
||
| func TestAccountResourceTypes_ParseNegative(t *testing.T) { | ||
| _, err := parseAccountResourceTypes("scoz") // Here 'z' is invalid | ||
| require.NotNil(t, err) | ||
| require.Contains(t, err.Error(), "122") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.