@@ -6,6 +6,7 @@ package storage
6
6
import (
7
7
"context"
8
8
"net/http"
9
+ "net/http/httptest"
9
10
"os"
10
11
"testing"
11
12
@@ -92,3 +93,106 @@ func TestS3StorageBadRequest(t *testing.T) {
92
93
_ , err := NewStorage (setting .MinioStorageType , cfg )
93
94
assert .ErrorContains (t , err , message )
94
95
}
96
+
97
+ func TestMinioCredentials (t * testing.T ) {
98
+ const (
99
+ ExpectedAccessKey = "ExampleAccessKeyID"
100
+ ExpectedSecretAccessKey = "ExampleSecretAccessKeyID"
101
+ // Use a FakeEndpoint for IAM credentials to avoid logging any
102
+ // potential real IAM credentials when running in EC2.
103
+ FakeEndpoint = "http://localhost"
104
+ )
105
+
106
+ t .Run ("Static Credentials" , func (t * testing.T ) {
107
+ cfg := setting.MinioStorageConfig {
108
+ AccessKeyID : ExpectedAccessKey ,
109
+ SecretAccessKey : ExpectedSecretAccessKey ,
110
+ }
111
+ creds := buildMinioCredentials (cfg , FakeEndpoint )
112
+ v , err := creds .Get ()
113
+
114
+ assert .NoError (t , err )
115
+ assert .Equal (t , ExpectedAccessKey , v .AccessKeyID )
116
+ assert .Equal (t , ExpectedSecretAccessKey , v .SecretAccessKey )
117
+ })
118
+
119
+ t .Run ("Chain" , func (t * testing.T ) {
120
+ cfg := setting.MinioStorageConfig {}
121
+
122
+ t .Run ("EnvMinio" , func (t * testing.T ) {
123
+ t .Setenv ("MINIO_ACCESS_KEY" , ExpectedAccessKey + "Minio" )
124
+ t .Setenv ("MINIO_SECRET_KEY" , ExpectedSecretAccessKey + "Minio" )
125
+
126
+ creds := buildMinioCredentials (cfg , FakeEndpoint )
127
+ v , err := creds .Get ()
128
+
129
+ assert .NoError (t , err )
130
+ assert .Equal (t , ExpectedAccessKey + "Minio" , v .AccessKeyID )
131
+ assert .Equal (t , ExpectedSecretAccessKey + "Minio" , v .SecretAccessKey )
132
+ })
133
+
134
+ t .Run ("EnvAWS" , func (t * testing.T ) {
135
+ t .Setenv ("AWS_ACCESS_KEY" , ExpectedAccessKey + "AWS" )
136
+ t .Setenv ("AWS_SECRET_KEY" , ExpectedSecretAccessKey + "AWS" )
137
+
138
+ creds := buildMinioCredentials (cfg , FakeEndpoint )
139
+ v , err := creds .Get ()
140
+
141
+ assert .NoError (t , err )
142
+ assert .Equal (t , ExpectedAccessKey + "AWS" , v .AccessKeyID )
143
+ assert .Equal (t , ExpectedSecretAccessKey + "AWS" , v .SecretAccessKey )
144
+ })
145
+
146
+ t .Run ("FileMinio" , func (t * testing.T ) {
147
+ t .Setenv ("MINIO_SHARED_CREDENTIALS_FILE" , "testdata/minio.json" )
148
+ // prevent loading any actual credentials files from the user
149
+ t .Setenv ("AWS_SHARED_CREDENTIALS_FILE" , "testdata/fake" )
150
+
151
+ creds := buildMinioCredentials (cfg , FakeEndpoint )
152
+ v , err := creds .Get ()
153
+
154
+ assert .NoError (t , err )
155
+ assert .Equal (t , ExpectedAccessKey + "MinioFile" , v .AccessKeyID )
156
+ assert .Equal (t , ExpectedSecretAccessKey + "MinioFile" , v .SecretAccessKey )
157
+ })
158
+
159
+ t .Run ("FileAWS" , func (t * testing.T ) {
160
+ // prevent loading any actual credentials files from the user
161
+ t .Setenv ("MINIO_SHARED_CREDENTIALS_FILE" , "testdata/fake.json" )
162
+ t .Setenv ("AWS_SHARED_CREDENTIALS_FILE" , "testdata/aws_credentials" )
163
+
164
+ creds := buildMinioCredentials (cfg , FakeEndpoint )
165
+ v , err := creds .Get ()
166
+
167
+ assert .NoError (t , err )
168
+ assert .Equal (t , ExpectedAccessKey + "AWSFile" , v .AccessKeyID )
169
+ assert .Equal (t , ExpectedSecretAccessKey + "AWSFile" , v .SecretAccessKey )
170
+ })
171
+
172
+ t .Run ("IAM" , func (t * testing.T ) {
173
+ // prevent loading any actual credentials files from the user
174
+ t .Setenv ("MINIO_SHARED_CREDENTIALS_FILE" , "testdata/fake.json" )
175
+ t .Setenv ("AWS_SHARED_CREDENTIALS_FILE" , "testdata/fake" )
176
+
177
+ // Spawn a server to emulate the EC2 Instance Metadata
178
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
179
+ // The client will actually make 3 requests here,
180
+ // first will be to get the IMDSv2 token, second to
181
+ // get the role, and third for the actual
182
+ // credentials. However, we can return credentials
183
+ // every request since we're not emulating a full
184
+ // IMDSv2 flow.
185
+ w .Write ([]byte (`{"Code":"Success","AccessKeyId":"ExampleAccessKeyIDIAM","SecretAccessKey":"ExampleSecretAccessKeyIDIAM"}` ))
186
+ }))
187
+ defer server .Close ()
188
+
189
+ // Use the provided EC2 Instance Metadata server
190
+ creds := buildMinioCredentials (cfg , server .URL )
191
+ v , err := creds .Get ()
192
+
193
+ assert .NoError (t , err )
194
+ assert .Equal (t , ExpectedAccessKey + "IAM" , v .AccessKeyID )
195
+ assert .Equal (t , ExpectedSecretAccessKey + "IAM" , v .SecretAccessKey )
196
+ })
197
+ })
198
+ }
0 commit comments