-
-
Notifications
You must be signed in to change notification settings - Fork 336
Add support for AWS credential_process configuration, allowing external processes to provide temporary AWS credentials. #17864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| import ch.cyberduck.core.Credentials; | ||
| import ch.cyberduck.core.CredentialsConfigurator; | ||
| import ch.cyberduck.core.Factory; | ||
| import ch.cyberduck.core.Host; | ||
| import ch.cyberduck.core.Local; | ||
| import ch.cyberduck.core.LocalFactory; | ||
|
|
@@ -33,8 +34,11 @@ | |
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Instant; | ||
| import java.time.format.DateTimeParseException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Scanner; | ||
|
|
||
|
|
@@ -94,6 +98,40 @@ else if(StringUtils.equals(entry.getValue().getAwsAccessIdKey(), credentials.get | |
| return false; | ||
| }).map(Map.Entry::getValue).findFirst().orElse(StringUtils.isBlank(host.getCredentials().getUsername()) ? profiles.get("default") : null); | ||
| if(null != profile) { | ||
| if(profile.isProcessBasedProfile()) { | ||
| // Uses external process to retrieve temporary credentials | ||
| final String command = profile.getCredentialProcess(); | ||
| final ObjectMapper mapper = JsonMapper.builder() | ||
| .serializationInclusion(Include.NON_NULL) | ||
| .enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY) | ||
| .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
| .visibility(PropertyAccessor.FIELD, Visibility.ANY).build(); | ||
| List<String> cmd = new ArrayList<>(); | ||
| switch(Factory.Platform.getDefault()) { | ||
| case windows: | ||
| cmd.add("cmd"); | ||
| cmd.add("/c"); | ||
| break; | ||
| default: | ||
| cmd.add("sh"); | ||
| cmd.add("-c"); | ||
| break; | ||
| } | ||
| cmd.add(command); | ||
|
Comment on lines
+110
to
+120
|
||
| final ProcessBuilder builder = new ProcessBuilder(cmd); | ||
| try { | ||
| final Process process = builder.start(); | ||
| try(InputStream reader = process.getInputStream()) { | ||
| final CachedCredential cached = mapper.readValue(reader, CachedCredential.class); | ||
| return credentials.setTokens(new TemporaryAccessTokens( | ||
| cached.accessKey, cached.secretKey, cached.sessionToken, cached.getExpiration())); | ||
| } | ||
|
Comment on lines
121
to
128
|
||
| } | ||
| catch(IOException e) { | ||
| log.warn("Failure \"{}\" parsing cached credentials from {}", e.getMessage(), command); | ||
| return credentials; | ||
|
Comment on lines
+130
to
+132
|
||
| } | ||
| } | ||
| if(profile.isRoleBasedProfile()) { | ||
| log.debug("Configure credentials from role based profile {}", profile.getProfileName()); | ||
| if(StringUtils.isBlank(profile.getRoleSourceProfile())) { | ||
|
|
@@ -115,7 +153,7 @@ else if(!profiles.containsKey(profile.getRoleSourceProfile())) { | |
| } | ||
| // No further token exchange required | ||
| return credentials.setTokens(new TemporaryAccessTokens( | ||
| cached.accessKey, cached.secretKey, cached.sessionToken, Instant.parse(cached.expiration).toEpochMilli())); | ||
| cached.accessKey, cached.secretKey, cached.sessionToken, cached.getExpiration())); | ||
| } | ||
| else { | ||
| // If a profile defines the role_arn property then the profile is treated as an assume role profile | ||
|
|
@@ -137,7 +175,7 @@ else if(!profiles.containsKey(profile.getRoleSourceProfile())) { | |
| return credentials; | ||
| } | ||
| return credentials.setTokens(new TemporaryAccessTokens( | ||
| cached.accessKey, cached.secretKey, cached.sessionToken, Instant.parse(cached.expiration).toEpochMilli())); | ||
| cached.accessKey, cached.secretKey, cached.sessionToken, cached.getExpiration())); | ||
| } | ||
| log.debug("Set credentials from profile {}", profile.getProfileName()); | ||
| return credentials | ||
|
|
@@ -241,7 +279,7 @@ private CachedCredential fetchSsoCredentials(final Map<String, String> propertie | |
| log.warn("Failure parsing SSO credentials."); | ||
| return null; | ||
| } | ||
| final Instant expiration = Instant.parse(cached.credentials.expiration); | ||
| final Instant expiration = Instant.ofEpochMilli(cached.credentials.getExpiration()); | ||
| if(expiration.isBefore(Instant.now())) { | ||
| log.warn("Expired AWS SSO credentials."); | ||
| return null; | ||
|
|
@@ -276,6 +314,15 @@ private static class CachedCredential { | |
| private String sessionToken; | ||
| @JsonProperty("Expiration") | ||
| private String expiration; | ||
|
|
||
| public Long getExpiration() { | ||
| try { | ||
| return Instant.parse(expiration).toEpochMilli(); | ||
| } | ||
| catch(DateTimeParseException e) { | ||
| return -1L; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New
credential_processsupport isn’t covered by tests, while this class already has dedicated unit tests and fixtures for other profile types. Add a test profile insrc/test/resources/.../.awsplus a test that runs a small helper command (or a mocked process runner) returning the expected JSON, and assert the parsed tokens/expiry are applied.