@@ -49,7 +49,7 @@ namespace {
49
49
constexpr absl::string_view kKeyUriPrefix = " aws-kms://" ;
50
50
51
51
// Returns AWS key ARN contained in `key_uri`. If `key_uri` does not refer to an
52
- // AWS key, returns an empty string .
52
+ // AWS key, returns an error .
53
53
util::StatusOr<std::string> GetKeyArn (absl::string_view key_uri) {
54
54
if (!absl::StartsWithIgnoreCase (key_uri, kKeyUriPrefix )) {
55
55
return util::Status (absl::StatusCode::kInvalidArgument ,
@@ -62,8 +62,8 @@ util::StatusOr<std::string> GetKeyArn(absl::string_view key_uri) {
62
62
// `key_arn`.
63
63
// An AWS key ARN is of the form
64
64
// arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab.
65
- util::StatusOr<Aws::Client::ClientConfiguration>
66
- GetAwsClientConfig ( absl::string_view key_arn) {
65
+ util::StatusOr<Aws::Client::ClientConfiguration> GetAwsClientConfig (
66
+ absl::string_view key_arn) {
67
67
std::vector<std::string> key_arn_parts = absl::StrSplit (key_arn, ' :' );
68
68
if (key_arn_parts.size () < 6 ) {
69
69
return util::Status (absl::StatusCode::kInvalidArgument ,
@@ -98,9 +98,9 @@ util::StatusOr<std::string> GetValue(absl::string_view name,
98
98
absl::string_view line) {
99
99
std::vector<std::string> parts = absl::StrSplit (line, ' =' );
100
100
if (parts.size () != 2 || absl::StripAsciiWhitespace (parts[0 ]) != name) {
101
- return util::Status (
102
- absl::StatusCode:: kInvalidArgument ,
103
- absl::StrCat ( " Expected line in format " , name, " = value" ));
101
+ return util::Status (absl::StatusCode:: kInvalidArgument ,
102
+ absl::StrCat ( " Expected line to have the format: " , name ,
103
+ " = value. Found: " , line ));
104
104
}
105
105
return std::string (absl::StripAsciiWhitespace (parts[1 ]));
106
106
}
@@ -132,40 +132,42 @@ util::StatusOr<std::string> GetValue(absl::string_view name,
132
132
// Aws::Auth::ProfileConfigFileAWSCredentialsProvider.
133
133
util::StatusOr<Aws::Auth::AWSCredentials> GetAwsCredentials (
134
134
absl::string_view credentials_path) {
135
- if (!credentials_path.empty ()) { // Read credentials from given file.
136
- auto creds_result = ReadFile (std::string (credentials_path));
137
- if (!creds_result.ok ()) {
138
- return creds_result.status ();
139
- }
140
- std::vector<std::string> creds_lines =
141
- absl::StrSplit (creds_result.value (), ' \n ' );
142
- if (creds_lines.size () < 3 ) {
143
- return util::Status (absl::StatusCode::kInvalidArgument ,
144
- absl::StrCat (" Invalid format of credentials in file " ,
145
- credentials_path));
146
- }
147
- auto key_id_result = GetValue (" aws_access_key_id" , creds_lines[1 ]);
148
- if (!key_id_result.ok ()) {
149
- return util::Status (absl::StatusCode::kInvalidArgument ,
150
- absl::StrCat (" Invalid format of credentials in file " ,
151
- credentials_path, " : " ,
152
- key_id_result.status ().message ()));
153
- }
154
- auto secret_key_result = GetValue (" aws_secret_access_key" , creds_lines[2 ]);
155
- if (!secret_key_result.ok ()) {
156
- return util::Status (
157
- absl::StatusCode::kInvalidArgument ,
158
- absl::StrCat (" Invalid format of credentials in file " ,
159
- credentials_path, " : " ,
160
- secret_key_result.status ().message ()));
161
- }
162
- return Aws::Auth::AWSCredentials (key_id_result.value ().c_str (),
163
- secret_key_result.value ().c_str ());
135
+ if (credentials_path.empty ()) {
136
+ // Get default credentials.
137
+ Aws::Auth::DefaultAWSCredentialsProviderChain provider_chain;
138
+ return provider_chain.GetAWSCredentials ();
164
139
}
165
-
166
- // Get default credentials.
167
- Aws::Auth::DefaultAWSCredentialsProviderChain provider_chain;
168
- return provider_chain.GetAWSCredentials ();
140
+ // Read credentials from the given file.
141
+ util::StatusOr<std::string> creds_result =
142
+ ReadFile (std::string (credentials_path));
143
+ if (!creds_result.ok ()) {
144
+ return creds_result.status ();
145
+ }
146
+ std::vector<std::string> creds_lines =
147
+ absl::StrSplit (creds_result.value (), ' \n ' );
148
+ if (creds_lines.size () < 3 ) {
149
+ return util::Status (absl::StatusCode::kInvalidArgument ,
150
+ absl::StrCat (" Invalid format of credentials in file " ,
151
+ credentials_path));
152
+ }
153
+ util::StatusOr<std::string> key_id_result =
154
+ GetValue (" aws_access_key_id" , creds_lines[1 ]);
155
+ if (!key_id_result.ok ()) {
156
+ return util::Status (
157
+ absl::StatusCode::kInvalidArgument ,
158
+ absl::StrCat (" Invalid format of credentials in file " , credentials_path,
159
+ " : " , key_id_result.status ().message ()));
160
+ }
161
+ util::StatusOr<std::string> secret_key_result =
162
+ GetValue (" aws_secret_access_key" , creds_lines[2 ]);
163
+ if (!secret_key_result.ok ()) {
164
+ return util::Status (
165
+ absl::StatusCode::kInvalidArgument ,
166
+ absl::StrCat (" Invalid format of credentials in file " , credentials_path,
167
+ " : " , secret_key_result.status ().message ()));
168
+ }
169
+ return Aws::Auth::AWSCredentials (key_id_result.value ().c_str (),
170
+ secret_key_result.value ().c_str ());
169
171
}
170
172
171
173
void InitAwsApi () {
@@ -225,26 +227,22 @@ bool AwsKmsClient::DoesSupport(absl::string_view key_uri) const {
225
227
return key_arn_.empty () ? true : key_arn_ == *key_arn;
226
228
}
227
229
228
- util::StatusOr<std::unique_ptr<Aead>>
229
- AwsKmsClient::GetAead (absl::string_view key_uri) const {
230
- if (!DoesSupport (key_uri)) {
231
- if (!key_arn_.empty ()) {
230
+ util::StatusOr<std::unique_ptr<Aead>> AwsKmsClient::GetAead (
231
+ absl::string_view key_uri) const {
232
+ util::StatusOr<std::string> key_arn = GetKeyArn (key_uri);
233
+ if (!key_arn.ok ()) {
234
+ return key_arn.status ();
235
+ }
236
+ // This client is bound to a specific key.
237
+ if (!key_arn_.empty ()) {
238
+ if (key_arn_ != *key_arn) {
232
239
return util::Status (absl::StatusCode::kInvalidArgument ,
233
240
absl::StrCat (" This client is bound to " , key_arn_,
234
241
" and cannot use key " , key_uri));
235
242
}
236
- return util::Status (
237
- absl::StatusCode::kInvalidArgument ,
238
- absl::StrCat (" This client does not support key " , key_uri));
239
- }
240
-
241
- // This client is bound to a specific key.
242
- if (!key_arn_.empty ()) {
243
243
return AwsKmsAead::New (key_arn_, aws_client_);
244
244
}
245
245
246
- // Create an Aws::KMS::KMSClient for the given key.
247
- util::StatusOr<std::string> key_arn = GetKeyArn (key_uri);
248
246
util::StatusOr<Aws::Client::ClientConfiguration> client_config =
249
247
GetAwsClientConfig (*key_arn);
250
248
if (!client_config.ok ()) {
0 commit comments