-
Notifications
You must be signed in to change notification settings - Fork 88
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
Added obtaining of IAM roles. #24
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
@@ -79,14 +80,64 @@ protected Set<GrantedAuthority> getAuthorities(String username) throws Authentic | |
return authorities; | ||
} | ||
|
||
private Collection<String> getRoles(String username) { | ||
/** | ||
* Lists roles of AWS from IAM profile of pointed user. | ||
* | ||
* @param userName given userName in AWS | ||
* @param showErrorOutput flag marking outputing errors to console | ||
* @return collection with user's roles | ||
*/ | ||
public static Collection<String> obtainRoles(String userName, boolean showErrorOutput) { | ||
Collection<String> roles = new ArrayList<String>(); | ||
|
||
final String baseCommand = "aws iam list-groups-for-user --user-name"; | ||
String command = String.format("%s %s", baseCommand, userName); | ||
|
||
BufferedReader reader = null; | ||
Process process; | ||
try { | ||
process = Runtime.getRuntime().exec(command); | ||
process.waitFor(); | ||
reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
|
||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
String theLine = line.trim(); | ||
if (theLine.startsWith("\"GroupName\": ")) { | ||
String roleName = theLine.substring(14, theLine.length() - 1); | ||
roles.add(roleName); | ||
} | ||
} | ||
} catch (Exception exc_1) { | ||
if (showErrorOutput) { | ||
exc_1.printStackTrace(); | ||
} | ||
} finally { | ||
if (reader != null) { | ||
try { | ||
reader.close(); | ||
} catch (Exception exc_2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could use try-with-resource syntax to reduce your code and make it simpler and more compact. |
||
if (showErrorOutput) { | ||
System.err.println("Couldn't even close the BufferedReader."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Collection<String> roles = new ArrayList<>(); | ||
// TODO for a reasonable application you need to retrieve the roles of the user from a central IAM system | ||
roles.add(username); | ||
return roles; | ||
} | ||
|
||
/** | ||
* Return list of IAM roles. | ||
* | ||
* @param username pointed user's name | ||
* @return obtained roles | ||
*/ | ||
private Collection<String> getRoles(String username) { | ||
|
||
return this.obtainRoles(username); | ||
} | ||
|
||
/** | ||
* @return amBuilder | ||
*/ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
so you have changed the app to connect to AWS. Nice feature.
But should we have that by default? What if project does not want to use AWS? What if "aws" command is not available?
Do not get me wrong: I love the idea, but this could be an option or maybe we need more like a documentation having that as a code-snippet, or we could add some minimum module code for this that can be opted in.
See also:
oasp/oasp4j#633