Skip to content

Commit 1dfb1f0

Browse files
authored
Add durationSeconds and policy support in LdapIdentityProvider. (#1209)
Fixes #1210 Signed-off-by: Bala.FA <bala.gluster@gmail.com>
1 parent 24b52bd commit 1dfb1f0

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

api/src/main/java/io/minio/credentials/AssumeRoleBaseProvider.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.security.ProviderException;
2323
import java.util.Arrays;
24+
import java.util.concurrent.TimeUnit;
2425
import okhttp3.HttpUrl;
2526
import okhttp3.OkHttpClient;
2627
import okhttp3.Protocol;
@@ -29,6 +30,7 @@
2930

3031
/** Base class to AssumeRole based providers. */
3132
public abstract class AssumeRoleBaseProvider implements Provider {
33+
public static final int DEFAULT_DURATION_SECONDS = (int) TimeUnit.HOURS.toSeconds(1);
3234
private final OkHttpClient httpClient;
3335
private Credentials credentials;
3436

@@ -59,6 +61,12 @@ public synchronized Credentials fetch() {
5961
}
6062
}
6163

64+
protected static int getValidDurationSeconds(Integer duration) {
65+
return (duration != null && duration > DEFAULT_DURATION_SECONDS)
66+
? duration
67+
: DEFAULT_DURATION_SECONDS;
68+
}
69+
6270
protected HttpUrl.Builder newUrlBuilder(
6371
HttpUrl url,
6472
String action,

api/src/main/java/io/minio/credentials/AssumeRoleProvider.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.security.ProviderException;
2525
import java.time.ZonedDateTime;
2626
import java.util.Objects;
27-
import java.util.concurrent.TimeUnit;
2827
import javax.annotation.Nonnull;
2928
import javax.annotation.Nullable;
3029
import okhttp3.HttpUrl;
@@ -43,7 +42,6 @@
4342
* API</a>.
4443
*/
4544
public class AssumeRoleProvider extends AssumeRoleBaseProvider {
46-
public static final int DEFAULT_DURATION_SECONDS = (int) TimeUnit.HOURS.toSeconds(1);
4745
private final String accessKey;
4846
private final String secretKey;
4947
private final String region;
@@ -77,11 +75,6 @@ public AssumeRoleProvider(
7775
throw new IllegalArgumentException("Length of ExternalId must be in between 2 and 1224");
7876
}
7977

80-
durationSeconds =
81-
(durationSeconds != null && durationSeconds > DEFAULT_DURATION_SECONDS)
82-
? durationSeconds
83-
: DEFAULT_DURATION_SECONDS;
84-
8578
String host = url.host() + ":" + url.port();
8679
// ignore port when port and service matches i.e HTTP -> 80, HTTPS -> 443
8780
if ((url.scheme().equals("http") && url.port() == 80)
@@ -90,7 +83,13 @@ public AssumeRoleProvider(
9083
}
9184

9285
HttpUrl.Builder urlBuilder =
93-
newUrlBuilder(url, "AssumeRole", durationSeconds, policy, roleArn, roleSessionName);
86+
newUrlBuilder(
87+
url,
88+
"AssumeRole",
89+
getValidDurationSeconds(durationSeconds),
90+
policy,
91+
roleArn,
92+
roleSessionName);
9493
if (externalId != null) {
9594
urlBuilder.addQueryParameter("ExternalId", externalId);
9695
}

api/src/main/java/io/minio/credentials/LdapIdentityProvider.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public LdapIdentityProvider(
4343
@Nonnull String stsEndpoint,
4444
@Nonnull String ldapUsername,
4545
@Nonnull String ldapPassword,
46+
@Nullable Integer durationSeconds,
47+
@Nullable String policy,
4648
@Nullable OkHttpClient customHttpClient) {
4749
super(customHttpClient);
4850
stsEndpoint = Objects.requireNonNull(stsEndpoint, "STS endpoint cannot be empty");
@@ -53,7 +55,13 @@ public LdapIdentityProvider(
5355
Objects.requireNonNull(ldapPassword, "LDAP password must not be null");
5456

5557
HttpUrl.Builder urlBuilder =
56-
newUrlBuilder(url, "AssumeRoleWithLDAPIdentity", 0, null, null, null);
58+
newUrlBuilder(
59+
url,
60+
"AssumeRoleWithLDAPIdentity",
61+
getValidDurationSeconds(durationSeconds),
62+
policy,
63+
null,
64+
null);
5765
url =
5866
urlBuilder
5967
.addQueryParameter("LDAPUsername", ldapUsername)
@@ -62,6 +70,14 @@ public LdapIdentityProvider(
6270
this.request = new Request.Builder().url(url).method("POST", EMPTY_BODY).build();
6371
}
6472

73+
public LdapIdentityProvider(
74+
@Nonnull String stsEndpoint,
75+
@Nonnull String ldapUsername,
76+
@Nonnull String ldapPassword,
77+
@Nullable OkHttpClient customHttpClient) {
78+
this(stsEndpoint, ldapUsername, ldapPassword, null, null, customHttpClient);
79+
}
80+
6581
@Override
6682
protected Request getRequest() {
6783
return this.request;

0 commit comments

Comments
 (0)