-
Notifications
You must be signed in to change notification settings - Fork 3
/
iam.go
156 lines (125 loc) · 4.23 KB
/
iam.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
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
package core
import (
"crypto/rand"
"encoding/base64"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/minio/minio-go/v6/pkg/credentials"
"github.com/minio/minio/pkg/auth"
"github.com/rs/zerolog/log"
)
type RefreshTokenStruct struct {
RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"`
}
type IAMClientConfig struct {
CallbackURL string
Host string
Port int
ClientName string
}
type ClientResponse struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Endpoint string `json:"registration_client_uri"`
}
// Returns a base64 encoded random 32 byte string.
func RandomState() string {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return base64.RawURLEncoding.EncodeToString(b)
}
// IAMProvider credential provider for oidc
type IAMProvider struct {
StsEndpoint string
HTTPClient *http.Client
Token string
Creds *AssumeRoleWithWebIdentityResponse
RefreshTokenRenew int
}
// AssumeRoleWithWebIdentityResponse the struct of the STS WebIdentity call response.
type AssumeRoleWithWebIdentityResponse struct {
XMLName xml.Name `xml:"https://sts.amazonaws.com/doc/2011-06-15/ AssumeRoleWithWebIdentityResponse" json:"-"`
Result WebIdentityResult `xml:"AssumeRoleWithWebIdentityResult"`
ResponseMetadata struct {
RequestID string `xml:"RequestId,omitempty"`
} `xml:"ResponseMetadata,omitempty"`
}
// AssumedRoleUser - The identifiers for the temporary security credentials that
// the operation returns. Please also see https://docs.aws.amazon.com/goto/WebAPI/sts-2011-06-15/AssumedRoleUser
type AssumedRoleUser struct {
Arn string
AssumedRoleID string `xml:"AssumeRoleId"`
// contains filtered or unexported fields
}
// WebIdentityResult - Contains the response to a successful AssumeRoleWithWebIdentity
// request, including temporary credentials that can be used to make MinIO API requests.
type WebIdentityResult struct {
AssumedRoleUser AssumedRoleUser `xml:",omitempty"`
Audience string `xml:",omitempty"`
Credentials auth.Credentials `xml:",omitempty"`
PackedPolicySize int `xml:",omitempty"`
Provider string `xml:",omitempty"`
SubjectFromWebIdentityToken string `xml:",omitempty"`
}
// Retrieve credentials.
func (t *IAMProvider) Retrieve() (credentials.Value, error) {
log.Info().Msg("IAM - Retrieve")
body := url.Values{}
body.Set("Action", "AssumeRoleWithWebIdentity")
body.Set("Version", "2011-06-15")
body.Set("WebIdentityToken", t.Token)
body.Set("DurationSeconds", strconv.Itoa(t.RefreshTokenRenew*60))
log.Info().Str("stsEndpoint", t.StsEndpoint).Str("body", body.Encode()).Msg("IAM")
url, errParse := url.Parse(
strings.Join(
[]string{t.StsEndpoint, "?", body.Encode()},
"",
),
)
if errParse != nil {
panic(errParse)
}
log.Info().Str("url", url.String()).Msg("IAM")
req := http.Request{ // nolint:exhaustivestruct
Method: "POST",
URL: url,
}
resp, errDo := t.HTTPClient.Do(&req)
if errDo != nil {
log.Err(errDo).Msg("IAM")
return credentials.Value{}, fmt.Errorf("IAM retrieve %w", errDo)
}
defer resp.Body.Close()
log.Info().Int("statusCode", resp.StatusCode).Str("status", resp.Status).Msg("IAM")
rbody, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
log.Err(errRead).Msg("IAM")
return credentials.Value{}, fmt.Errorf("IAM retrieve %w", errRead)
}
log.Info().Str("body", string(rbody)).Msg("IAM")
t.Creds = &AssumeRoleWithWebIdentityResponse{}
errUnmarshall := xml.Unmarshal(rbody, t.Creds)
if errUnmarshall != nil {
log.Err(errUnmarshall).Msg("IAM")
return credentials.Value{}, fmt.Errorf("IAM retrieve %w", errUnmarshall)
}
log.Info().Str("credential", "acquired").Msg("IAM")
return credentials.Value{ // nolint:exhaustivestruct
AccessKeyID: t.Creds.Result.Credentials.AccessKey,
SecretAccessKey: t.Creds.Result.Credentials.SecretKey,
SessionToken: t.Creds.Result.Credentials.SessionToken,
}, nil
}
// IsExpired test.
func (t *IAMProvider) IsExpired() bool {
return t.Creds.Result.Credentials.IsExpired()
}