Skip to content
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

fix: remove erroneous check for the subject token field name for text credential source #822

Merged
merged 4 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,21 @@ enum CredentialFormatType {
Map<String, String> formatMap = (Map<String, String>) credentialSourceMap.get("format");
if (formatMap != null && formatMap.containsKey("type")) {
String type = formatMap.get("type");
if (!"text".equals(type) && !"json".equals(type)) {
throw new IllegalArgumentException(
String.format("Invalid credential source format type: %s.", type));
}
credentialFormatType =
type.equals("text") ? CredentialFormatType.TEXT : CredentialFormatType.JSON;

if (!formatMap.containsKey("subject_token_field_name")) {
if ("json".equals(type)) {
lsirac marked this conversation as resolved.
Show resolved Hide resolved
// For JSON, the subject_token field name must be provided.
if (!formatMap.containsKey("subject_token_field_name")) {
throw new IllegalArgumentException(
"When specifying a JSON credential type, the subject_token_field_name must be set.");
}
credentialFormatType = CredentialFormatType.JSON;
subjectTokenFieldName = formatMap.get("subject_token_field_name");
} else if ("text".equals(type)) {
lsirac marked this conversation as resolved.
Show resolved Hide resolved
credentialFormatType = CredentialFormatType.TEXT;
} else {
throw new IllegalArgumentException(
"When specifying a JSON credential type, the subject_token_field_name must be set.");
String.format("Invalid credential source format type: %s.", type));
}
subjectTokenFieldName = formatMap.get("subject_token_field_name");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
lsirac marked this conversation as resolved.
Show resolved Hide resolved
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -415,6 +412,49 @@ void refreshAccessToken_workforceWithServiceAccountImpersonation() throws IOExce
assertEquals(expectedInternalOptions.toString(), query.get("options"));
}

@Test
void identityPoolCredentialSource_validFormats() {
Map<String, Object> credentialSourceMapWithFileTextSource = new HashMap<>();
Map<String, Object> credentialSourceMapWithFileJsonTextSource = new HashMap<>();
Map<String, Object> credentialSourceMapWithUrlTextSource = new HashMap<>();
Map<String, Object> credentialSourceMapWithUrlJsonTextSource = new HashMap<>();

credentialSourceMapWithFileTextSource.put("file", "/path/to/file");
credentialSourceMapWithFileJsonTextSource.put("file", "/path/to/file");

credentialSourceMapWithUrlTextSource.put("url", "https://google.com");
credentialSourceMapWithUrlJsonTextSource.put("url", "https://google.com");
Map<String, String> headersMap = new HashMap<>();
headersMap.put("header1", "value1");
headersMap.put("header2", "value2");
credentialSourceMapWithUrlTextSource.put("headers", headersMap);
credentialSourceMapWithUrlJsonTextSource.put("headers", headersMap);

Map<String, String> textFormat = new HashMap<>();
textFormat.put("type", "text");

Map<String, String> jsonTextFormat = new HashMap<>();
jsonTextFormat.put("type", "json");
lsirac marked this conversation as resolved.
Show resolved Hide resolved
jsonTextFormat.put("subject_token_field_name", "access_token");

credentialSourceMapWithFileTextSource.put("format", textFormat);
credentialSourceMapWithFileJsonTextSource.put("format", jsonTextFormat);

credentialSourceMapWithUrlTextSource.put("format", textFormat);
credentialSourceMapWithUrlJsonTextSource.put("format", jsonTextFormat);

List<Map<String, Object>> sources =
Arrays.asList(
credentialSourceMapWithFileTextSource,
credentialSourceMapWithFileJsonTextSource,
credentialSourceMapWithUrlTextSource,
credentialSourceMapWithUrlJsonTextSource);
for (Map<String, Object> source : sources) {
// Should not throw.
new IdentityPoolCredentialSource(source);
}
}

@Test
void identityPoolCredentialSource_invalidSourceType() {
IllegalArgumentException exception =
Expand Down