Skip to content

Commit aad57a9

Browse files
authored
Merge pull request #17 from uc4w6c/feature/set-session-name
feat: Set session name when assuming role
2 parents aa87619 + 392b040 commit aad57a9

6 files changed

Lines changed: 35 additions & 13 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = com.github.uc4w6c.bedrockassistant
44
pluginName = BedrockAssistant
55
pluginRepositoryUrl = https://github.com/uc4w6c/BedrockAssistant
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.1.2
7+
pluginVersion = 0.1.3
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 242

src/main/java/com/github/uc4w6c/bedrockassistant/dao/AssumeRoleDao.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public class AssumeRoleDao {
1515
public AssumeRoleResponse getToken(AssumeRoleRequestEntity requestEntity) {
1616
AssumeRoleRequest.Builder requestBuilder = AssumeRoleRequest
1717
.builder()
18-
.roleArn(requestEntity.roleArn())
19-
.roleSessionName(UUID.randomUUID().toString());
18+
.roleArn(requestEntity.roleArn());
2019

2120
if (requestEntity.mfaSerial().isPresent()) {
2221
requestBuilder.serialNumber(requestEntity.mfaSerial().get());
2322
requestBuilder.tokenCode(requestEntity.tokenCode().get());
2423
}
24+
requestBuilder.roleSessionName(
25+
requestEntity.roleSessionName()
26+
.orElse(UUID.randomUUID().toString()));
2527
AssumeRoleRequest request = requestBuilder.build();
2628

2729
StsClientBuilder stsClientBuilder = StsClient.builder();

src/main/java/com/github/uc4w6c/bedrockassistant/dao/entity/AssumeRoleRequestEntity.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ public record AssumeRoleRequestEntity(
66
String roleArn,
77
Optional<String> assumeRoleRegion,
88
Optional<String> mfaSerial,
9-
Optional<String> tokenCode
9+
Optional<String> tokenCode,
10+
Optional<String> roleSessionName
1011
) {
1112
public static class Builder {
1213
String roleArn = null;
1314
Optional<String> assumeRoleRegion = Optional.empty();
1415
Optional<String> mfaSerial = Optional.empty();
1516
Optional<String> tokenCode = Optional.empty();
17+
Optional<String> roleSessionName = Optional.empty();
1618

1719
public Builder roleArn(String roleArn) {
1820
this.roleArn = roleArn;
@@ -34,6 +36,11 @@ public Builder tokenCode(String tokenCode) {
3436
return this;
3537
}
3638

39+
public Builder roleSessionName(String roleSessionName) {
40+
this.roleSessionName = Optional.of(roleSessionName);
41+
return this;
42+
}
43+
3744
public AssumeRoleRequestEntity build() {
3845
if (this.roleArn == null) throw new IllegalArgumentException();
3946
if (this.mfaSerial.isPresent() != this.tokenCode.isPresent()) throw new IllegalArgumentException();
@@ -42,7 +49,8 @@ public AssumeRoleRequestEntity build() {
4249
this.roleArn,
4350
this.assumeRoleRegion,
4451
this.mfaSerial,
45-
this.tokenCode);
52+
this.tokenCode,
53+
this.roleSessionName);
4654
}
4755
}
4856
}

src/main/java/com/github/uc4w6c/bedrockassistant/domain/AwsProfile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
* @param region
1010
* @param roleArn
1111
* @param mfaSerial
12+
* @param roleSessionName
1213
*/
1314
public record AwsProfile(
1415
String name,
1516
Optional<String> region,
1617
Optional<String> roleArn,
17-
Optional<String> mfaSerial
18+
Optional<String> mfaSerial,
19+
Optional<String> roleSessionName
1820
) {
1921
@Override
2022
public boolean equals(Object o) {

src/main/java/com/github/uc4w6c/bedrockassistant/repository/AssumeRoleRepository.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public AwsCredentials getTokenWithMfa(AwsProfile profile, String tokenCode) {
2727
if (profile.region().isPresent()) {
2828
builder.assumeRoleRegion(profile.region().get());
2929
}
30+
if (profile.roleSessionName().isPresent()) {
31+
builder.roleSessionName(profile.roleSessionName().get());
32+
}
3033
builder.mfaSerial(profile.mfaSerial().get());
3134
builder.tokenCode(tokenCode);
3235

@@ -39,11 +42,16 @@ public AwsCredentials getTokenWithMfa(AwsProfile profile, String tokenCode) {
3942
}
4043

4144
public AwsCredentials getToken(AwsProfile profile) {
42-
AssumeRoleRequestEntity request = new AssumeRoleRequestEntity.Builder()
43-
.roleArn(profile.roleArn().get())
44-
.build();
45+
AssumeRoleRequestEntity.Builder builder = new AssumeRoleRequestEntity.Builder();
46+
builder.roleArn(profile.roleArn().get());
47+
if (profile.region().isPresent()) {
48+
builder.assumeRoleRegion(profile.region().get());
49+
}
50+
if (profile.roleSessionName().isPresent()) {
51+
builder.roleSessionName(profile.roleSessionName().get());
52+
}
4553

46-
AssumeRoleResponse response = assumeRoleDao.getToken(request);
54+
AssumeRoleResponse response = assumeRoleDao.getToken(builder.build());
4755
return new AwsCredentials(
4856
response.credentials().accessKeyId(),
4957
response.credentials().secretAccessKey(),

src/main/java/com/github/uc4w6c/bedrockassistant/repository/ProfileRepository.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public Set<AwsProfile> getProfiles() {
2323
new AwsProfile(
2424
entry.getKey(),
2525
entry.getValue().property("region"),
26-
entry.getValue().property("role-arn"),
27-
entry.getValue().property("mfa-serial")))
26+
entry.getValue().property("role_arn"),
27+
entry.getValue().property("mfa_serial"),
28+
entry.getValue().property("role_session_name")))
2829
.collect(Collectors.toSet());
2930
}
3031

@@ -33,6 +34,7 @@ public Optional<AwsProfile> getProfile(String profileName) {
3334
return optionalProfile.map(profile -> new AwsProfile(profile.name(),
3435
profile.property("region"),
3536
profile.property("role_arn"),
36-
profile.property("mfa_serial")));
37+
profile.property("mfa_serial"),
38+
profile.property("role_session_name")));
3739
}
3840
}

0 commit comments

Comments
 (0)