aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-storage-blob-go/2018-03-28/azblob/zc_sas_account.go
blob: 8b51193f6a07d1ffaf03de2d8368e8ddc03e297c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package azblob

import (
    "bytes"
    "fmt"
    "strings"
    "time"
)

// AccountSASSignatureValues 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 AccountSASSignatureValues struct {
    Version       string      `param:"sv"`  // If not specified, this defaults to SASVersion
    Protocol      SASProtocol `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"`
    Services      string      `param:"ss"`  // Create by initializing AccountSASServices and then call String()
    ResourceTypes string      `param:"srt"` // Create by initializing AccountSASResourceTypes and then call String()
}

// NewSASQueryParameters uses an account's shared key credential to sign this signature values to produce
// the proper SAS query parameters.
func (v AccountSASSignatureValues) NewSASQueryParameters(sharedKeyCredential *SharedKeyCredential) SASQueryParameters {
    // https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS
    if v.ExpiryTime.IsZero() || v.Permissions == "" || v.ResourceTypes == "" || v.Services == "" {
        panic("Account SAS is missing at least one of these: ExpiryTime, Permissions, Service, or ResourceType")
    }
    if v.Version == "" {
        v.Version = SASVersion
    }
    perms := &AccountSASPermissions{}
    if err := perms.Parse(v.Permissions); err != nil {
        panic(err)
    }
    v.Permissions = perms.String()

    startTime, expiryTime := FormatTimesForSASSigning(v.StartTime, v.ExpiryTime)

    stringToSign := strings.Join([]string{
        sharedKeyCredential.AccountName(),
        v.Permissions,
        v.Services,
        v.ResourceTypes,
        startTime,
        expiryTime,
        v.IPRange.String(),
        string(v.Protocol),
        v.Version,
        ""}, // That right, the account SAS requires a terminating extra newline
        "\n")

    signature := sharedKeyCredential.ComputeHMACSHA256(stringToSign)
    p := SASQueryParameters{
        // 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:      v.Services,
        resourceTypes: v.ResourceTypes,

        // Calculated SAS signature
        signature: signature,
    }
    return p
}

// The AccountSASPermissions 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 AccountSASSignatureValues's Permissions field.
type AccountSASPermissions struct {
    Read, Write, Delete, List, Add, Create, Update, Process bool
}

// String produces the SAS permissions string for an Azure Storage account.
// Call this method to set AccountSASSignatureValues's Permissions field.
func (p AccountSASPermissions) 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.Add {
        buffer.WriteRune('a')
    }
    if p.Create {
        buffer.WriteRune('c')
    }
    if p.Update {
        buffer.WriteRune('u')
    }
    if p.Process {
        buffer.WriteRune('p')
    }
    return buffer.String()
}

// Parse initializes the AccountSASPermissions's fields from a string.
func (p *AccountSASPermissions) Parse(s string) error {
    *p = AccountSASPermissions{} // 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 'a':
            p.Add = true
        case 'c':
            p.Create = true
        case 'u':
            p.Update = true
        case 'p':
            p.Process = true
        default:
            return fmt.Errorf("Invalid permission character: '%v'", r)
        }
    }
    return nil
}

// The AccountSASServices type simplifies creating the services string for an Azure Storage Account SAS.
// Initialize an instance of this type and then call its String method to set AccountSASSignatureValues's Services field.
type AccountSASServices struct {
    Blob, Queue, File bool
}

// String produces the SAS services string for an Azure Storage account.
// Call this method to set AccountSASSignatureValues's Services field.
func (s AccountSASServices) String() string {
    var buffer bytes.Buffer
    if s.Blob {
        buffer.WriteRune('b')
    }
    if s.Queue {
        buffer.WriteRune('q')
    }
    if s.File {
        buffer.WriteRune('f')
    }
    return buffer.String()
}

// Parse initializes the AccountSASServices' fields from a string.
func (a *AccountSASServices) Parse(s string) error {
    *a = AccountSASServices{} // Clear out the flags
    for _, r := range s {
        switch r {
        case 'b':
            a.Blob = true
        case 'q':
            a.Queue = true
        case 'f':
            a.File = true
        default:
            return fmt.Errorf("Invalid service character: '%v'", r)
        }
    }
    return nil
}

// The AccountSASResourceTypes 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 AccountSASSignatureValues's ResourceTypes field.
type AccountSASResourceTypes struct {
    Service, Container, Object bool
}

// String produces the SAS resource types string for an Azure Storage account.
// Call this method to set AccountSASSignatureValues's ResourceTypes field.
func (rt AccountSASResourceTypes) 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()
}

// Parse initializes the AccountSASResourceType's fields from a string.
func (rt *AccountSASResourceTypes) Parse(s string) error {
    *rt = AccountSASResourceTypes{} // Clear out the flags
    for _, r := range s {
        switch r {
        case 's':
            rt.Service = true
        case 'q':
            rt.Container = true
        case 'o':
            rt.Object = true
        default:
            return fmt.Errorf("Invalid resource type: '%v'", r)
        }
    }
    return nil
}