forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsts.go
101 lines (81 loc) · 2.68 KB
/
sts.go
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
package cloudflare
import (
"errors"
"fmt"
"io"
"net/http"
"github.com/goccy/go-json"
"github.com/hashicorp/go-retryablehttp"
)
var (
ErrSTSFailure = errors.New("failed to fetch security token")
ErrSTSHTTPFailure = errors.New("failed making securtiy token issuer call")
ErrSTSHTTPResponseError = errors.New("security token request returned a failure")
ErrSTSMissingServiceSecret = errors.New("service secret missing but is required")
ErrSTSMissingServiceTag = errors.New("service tag missing but is required")
ErrSTSMissingIssuerHostname = errors.New("issuer hostname missing but is required")
ErrSTSMissingServicePath = errors.New("issuer path missing but is required")
)
// IssuerConfiguration allows the configuration of the issuance provider.
type IssuerConfiguration struct {
Hostname string
Path string
}
// SecurityTokenConfiguration holds the configuration for requesting a security
// token from the service.
type SecurityTokenConfiguration struct {
Issuer *IssuerConfiguration
ServiceTag string
Secret string
}
type securityToken struct {
Token string `json:"json_web_token"`
}
type securityTokenResponse struct {
Result securityToken `json:"result"`
Response
}
// fetchSTSCredentials provides a way to authenticate with the security token
// service and issue a usable token for the system.
func fetchSTSCredentials(stsConfig *SecurityTokenConfiguration) (string, error) {
if stsConfig.Secret == "" {
return "", ErrSTSMissingServiceSecret
}
if stsConfig.ServiceTag == "" {
return "", ErrSTSMissingServiceTag
}
if stsConfig.Issuer.Hostname == "" {
return "", ErrSTSMissingIssuerHostname
}
if stsConfig.Issuer.Path == "" {
return "", ErrSTSMissingServicePath
}
retryableClient := retryablehttp.NewClient()
retryableClient.RetryMax = 3
stsClient := retryableClient.StandardClient()
uri := fmt.Sprintf("https://%s%s", stsConfig.Issuer.Hostname, stsConfig.Issuer.Path)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return "", fmt.Errorf("HTTP request creation failed: %w", err)
}
req.Header.Set("Authorization", "Bearer "+stsConfig.ServiceTag+stsConfig.Secret)
resp, err := stsClient.Do(req)
if err != nil {
return "", ErrSTSHTTPFailure
}
var respBody []byte
respBody, err = io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
resp.Body.Close()
var stsTokenResponse *securityTokenResponse
err = json.Unmarshal(respBody, &stsTokenResponse)
if err != nil {
return "", fmt.Errorf("%s: %w", errUnmarshalError, err)
}
if !stsTokenResponse.Success {
return "", ErrSTSHTTPResponseError
}
return stsTokenResponse.Result.Token, nil
}