-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve AwsClientCredentials if not provided (#166)
* Resolve AwsClientCredentials if not provided * Fix bug in resolving, roll own credentials parser Co-authored-by: Jonathan Böcker <jonathan.bocker1@ingka.com>
- Loading branch information
Showing
13 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:shared_aws_api/src/credentials.dart'; | ||
|
||
class CredentialsUtil { | ||
static AwsClientCredentials resolve() => null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:shared_aws_api/src/credentials.dart'; | ||
import 'package:shared_aws_api/src/credentials/ini_config.dart'; | ||
|
||
class CredentialsUtil { | ||
static AwsClientCredentials resolve() { | ||
return fromEnvironment ?? fromProfileFile; | ||
} | ||
|
||
static AwsClientCredentials get fromEnvironment { | ||
final environment = Platform.environment; | ||
final accessKey = environment['AWS_ACCESS_KEY_ID']; | ||
final secretKey = environment['AWS_SECRET_ACCESS_KEY']; | ||
final sessionToken = environment['AWS_SESSION_TOKEN']; | ||
|
||
if (accessKey == null || secretKey == null) { | ||
return null; | ||
} | ||
|
||
return AwsClientCredentials( | ||
accessKey: accessKey, | ||
secretKey: secretKey, | ||
sessionToken: sessionToken, | ||
); | ||
} | ||
|
||
static String get userHome => | ||
Platform.environment['HOME'] ?? Platform.environment['USERPROFILE']; | ||
|
||
static AwsClientCredentials get fromProfileFile { | ||
final environment = Platform.environment; | ||
|
||
final credentialsFile = File( | ||
environment['AWS_SHARED_CREDENTIALS_FILE'] ?? | ||
'$userHome/.aws/credentials', | ||
); | ||
|
||
if (!credentialsFile.existsSync()) { | ||
return null; | ||
} | ||
|
||
final credentialsString = credentialsFile.readAsStringSync(); | ||
Config config; | ||
try { | ||
config = Config.fromString(credentialsString); | ||
} catch (e) { | ||
print('Parsing ${credentialsFile.path} failed: ${e.toString()}'); | ||
return null; | ||
} | ||
|
||
final profile = environment['AWS_PROFILE'] ?? 'default'; | ||
|
||
if (!config.hasSection(profile)) { | ||
print('${credentialsFile.path} does not contain [$profile] profile'); | ||
return null; | ||
} | ||
|
||
final secretKey = config.get(profile, 'aws_secret_access_key') ?? | ||
config.get(profile, 'aws_access_secret_key'); | ||
if (secretKey == null) { | ||
print('profile [$profile] does not contain "aws_secret_access_key"'); | ||
return null; | ||
} | ||
|
||
final accessKey = config.get(profile, 'aws_access_key_id'); | ||
if (accessKey == null) { | ||
print('profile [$profile] does not contain "aws_access_key_id"'); | ||
return null; | ||
} | ||
|
||
return AwsClientCredentials(accessKey: accessKey, secretKey: secretKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import 'dart:convert'; | ||
|
||
class Config { | ||
final Map<String, Map<String, String>> _sections = | ||
<String, Map<String, String>>{}; | ||
List<String> _strings; | ||
|
||
static final RegExp _blankLinePattern = RegExp(r'^\s*$'); | ||
static final RegExp _commentPattern = RegExp(r'^\s*[;#]'); | ||
static final RegExp _lineContinuationPattern = RegExp(r'^\s+'); | ||
static final RegExp _sectionPattern = RegExp(r'^\s*\[(.*\S.*)]\s*$'); | ||
static final RegExp _entryPattern = RegExp(r'^([^=]+)=(.*?)$'); | ||
|
||
static Iterable<String> _removeBlankLines(Iterable<String> source) => | ||
source.where((String line) => !_blankLinePattern.hasMatch(line)); | ||
|
||
static Iterable<String> _removeComments(Iterable<String> source) => | ||
source.where((String line) => !_commentPattern.hasMatch(line)); | ||
|
||
static List<String> _joinLongHeaderFields(Iterable<String> source) { | ||
final result = <String>[]; | ||
var line = ''; | ||
|
||
for (final current in source) { | ||
if (_lineContinuationPattern.hasMatch(current)) { | ||
line += current.replaceFirst(_lineContinuationPattern, ''); | ||
} else { | ||
if (line != '') { | ||
result.add(line); | ||
} | ||
line = current; | ||
} | ||
} | ||
if (line != '') { | ||
result.add(line); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static Config fromString(String string) => | ||
fromStrings(LineSplitter().convert(string)); | ||
|
||
static Config fromStrings(List<String> strings) { | ||
final config = Config() | ||
.._strings = | ||
_joinLongHeaderFields(_removeComments(_removeBlankLines(strings))); | ||
var section = 'default'; | ||
|
||
for (final current in config._strings) { | ||
final isSection = _sectionPattern.firstMatch(current); | ||
if (isSection != null) { | ||
section = isSection[1].trim(); | ||
config.addSection(section); | ||
} else { | ||
final isEntry = _entryPattern.firstMatch(current); | ||
if (isEntry != null) { | ||
config.set(section, isEntry[1].trim(), isEntry[2].trim()); | ||
} else { | ||
throw Exception('Unrecognized line: "$current"'); | ||
} | ||
} | ||
} | ||
return config; | ||
} | ||
|
||
Iterable<String> sections() => _sections.keys; | ||
|
||
void addSection(String name) { | ||
if (_sections.containsKey(name)) { | ||
throw Exception('DuplicateSectionError'); | ||
} | ||
_sections[name] = <String, String>{}; | ||
} | ||
|
||
bool hasSection(String name) => _sections.containsKey(name); | ||
|
||
Iterable<String> options(String name) => _getSection(name)?.keys; | ||
|
||
bool hasOption(String name, String option) => | ||
(_getSection(name) ?? {})?.containsKey(option) ?? false; | ||
|
||
String get(String name, String option) => (_getSection(name) ?? {})[option]; | ||
|
||
List<List<String>> items(String name) { | ||
final s = _getSection(name); | ||
return s != null | ||
? s.keys.map((String key) => [key, s[key]]).toList() | ||
: null; | ||
} | ||
|
||
void set(String name, String option, String value) { | ||
final s = _getSection(name); | ||
if (s == null) { | ||
throw Exception('NoSectionError'); | ||
} | ||
s[option] = value; | ||
} | ||
|
||
bool removeOption(String section, String option) { | ||
final s = _getSection(section); | ||
if (s != null) { | ||
if (s.containsKey(option)) { | ||
s.remove(option); | ||
return true; | ||
} | ||
return false; | ||
} | ||
throw Exception('NoSectionError'); | ||
} | ||
|
||
bool removeSection(String section) { | ||
if (_sections.containsKey(section)) { | ||
_sections.remove(section); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
Map<String, String> _getSection(String section) { | ||
if (_sections.containsKey(section)) { | ||
return _sections[section]; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters