-
Notifications
You must be signed in to change notification settings - Fork 232
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: JSON parsing of S2A addresses. #1589
Changes from 1 commit
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 | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -45,6 +45,7 @@ | |||||||||||||||||
import java.io.InputStream; | ||||||||||||||||||
import java.util.Arrays; | ||||||||||||||||||
import java.util.HashSet; | ||||||||||||||||||
import java.util.Map; | ||||||||||||||||||
import java.util.ServiceLoader; | ||||||||||||||||||
import java.util.Set; | ||||||||||||||||||
import javax.annotation.concurrent.ThreadSafe; | ||||||||||||||||||
|
@@ -59,6 +60,7 @@ | |||||||||||||||||
*/ | ||||||||||||||||||
@ThreadSafe | ||||||||||||||||||
public class SecureSessionAgent { | ||||||||||||||||||
static final String S2A_JSON_KEY = "s2a"; | ||||||||||||||||||
static final String S2A_PLAINTEXT_ADDRESS_JSON_KEY = "plaintext_address"; | ||||||||||||||||||
static final String S2A_MTLS_ADDRESS_JSON_KEY = "mtls_address"; | ||||||||||||||||||
static final String S2A_CONFIG_ENDPOINT_POSTFIX = | ||||||||||||||||||
|
@@ -190,15 +192,14 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() { | |||||||||||||||||
String mtlsS2AAddress = ""; | ||||||||||||||||||
try { | ||||||||||||||||||
plaintextS2AAddress = | ||||||||||||||||||
OAuth2Utils.validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); | ||||||||||||||||||
validateString(responseData, S2A_PLAINTEXT_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); | ||||||||||||||||||
} catch (IOException ignore) { | ||||||||||||||||||
/* | ||||||||||||||||||
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}. | ||||||||||||||||||
*/ | ||||||||||||||||||
} | ||||||||||||||||||
try { | ||||||||||||||||||
mtlsS2AAddress = | ||||||||||||||||||
OAuth2Utils.validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); | ||||||||||||||||||
mtlsS2AAddress = validateString(responseData, S2A_MTLS_ADDRESS_JSON_KEY, PARSE_ERROR_S2A); | ||||||||||||||||||
} catch (IOException ignore) { | ||||||||||||||||||
/* | ||||||||||||||||||
* Do not throw error because of parsing error, just leave the address as empty in {@link SecureSessionAgentConfig}. | ||||||||||||||||||
|
@@ -210,4 +211,23 @@ private SecureSessionAgentConfig getSecureSessionAgentConfigFromMDS() { | |||||||||||||||||
.setMtlsAddress(mtlsS2AAddress) | ||||||||||||||||||
.build(); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
private static String validateString(Map<String, Object> map, String key, String errorPrefix) | ||||||||||||||||||
throws IOException { | ||||||||||||||||||
Object value = map.get(S2A_JSON_KEY); | ||||||||||||||||||
if (value == null) { | ||||||||||||||||||
throw new IOException( | ||||||||||||||||||
String.format(OAuth2Utils.VALUE_NOT_FOUND_MESSAGE, errorPrefix, S2A_JSON_KEY)); | ||||||||||||||||||
} | ||||||||||||||||||
if (!(value instanceof Map)) { | ||||||||||||||||||
throw new IOException( | ||||||||||||||||||
String.format(OAuth2Utils.VALUE_WRONG_TYPE_MESSAGE, errorPrefix, "Map", S2A_JSON_KEY)); | ||||||||||||||||||
} | ||||||||||||||||||
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. qq, is this check necessary? Is it possible that it may be returned as an array? I was under the assumption that we had control over this API. Or is this just intended as a defensive check? 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. This is not really necessary, as you mention we own the MDS autoconfig endpoint. I have gotten rid of this check in the latest commit |
||||||||||||||||||
Object address = ((Map<String, Object>) value).get(key); | ||||||||||||||||||
if (!(address instanceof String)) { | ||||||||||||||||||
throw new IOException( | ||||||||||||||||||
String.format(OAuth2Utils.VALUE_WRONG_TYPE_MESSAGE, errorPrefix, "string", key)); | ||||||||||||||||||
} | ||||||||||||||||||
return (String) address; | ||||||||||||||||||
} | ||||||||||||||||||
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. If we can extract the above out, can we just revert back to using Oauth2Utils.validateString(...) and remove this method? This implementation looks to be the same as google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java Lines 151 to 158 in cc9826e
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. Yes, it is the same implementation, I extracted out the additional parsing logic into the calling function and reverted back to using |
||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -300,9 +300,7 @@ public LowLevelHttpResponse execute() throws IOException { | |
GenericJson content = new GenericJson(); | ||
content.setFactory(OAuth2Utils.JSON_FACTORY); | ||
if (requestStatusCode == 200) { | ||
for (Map.Entry<String, String> entrySet : s2aContentMap.entrySet()) { | ||
content.put(entrySet.getKey(), entrySet.getValue()); | ||
} | ||
content.put("s2a", s2aContentMap); | ||
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. nit: Can you use the constant from above? 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. Done in the latest commit. |
||
} | ||
String contentText = content.toPrettyString(); | ||
|
||
|
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.
Can retrieving the map be moved out to the calling method? It's shared by when parsing both plaintextS2AAdress and mtlsS2AAddress? If we do need
validateString
, then I think it should only be validating the inputted string.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.
Yes, we can do this, I've moved it out to the calling function in the latest commit.