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

Add HTTP Proxy support #233

Merged
merged 19 commits into from
Feb 4, 2021
Merged

Add HTTP Proxy support #233

merged 19 commits into from
Feb 4, 2021

Conversation

VitaliiTytarenko-okta
Copy link
Contributor

@VitaliiTytarenko-okta VitaliiTytarenko-okta commented Dec 23, 2020

Copy link
Contributor

@bdemers bdemers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, both the “standard” Java proxy props should work, and the ones used by the Okta SDK.

that way a user only has to specify the props once 🤔

@arvindkrishnakumar-okta
Copy link
Contributor

arvindkrishnakumar-okta commented Dec 28, 2020

IMHO, both the “standard” Java proxy props should work, and the ones used by the Okta SDK.

that way a user only has to specify the props once 🤔

@VitaliiTytarenko-okta You might want to check out https://github.com/okta/okta-spring-boot#proxy to see if it helps. In theory, setting these JVM properties should work.

@VitaliiTytarenko-okta
Copy link
Contributor Author

IMHO, both the “standard” Java proxy props should work, and the ones used by the Okta SDK.

that way a user only has to specify the props once 🤔

As I mentioned here #57, I think, it's a bad idea to mix these properties.

@bdemers
Copy link
Contributor

bdemers commented Jan 12, 2021

We should standardize on okta.oauth2.proxy.* That makes it mirror what is used for the SDKs

Re: the property names, other libraries use the Java proxy properties or directly use the ProxySelector (IIRC OK HTTP uses the ProxySelector).

HttpClient also has this implementation: https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/SystemDefaultHttpClient.html

@VitaliiTytarenko-okta
Copy link
Contributor Author

@bdemers
Could you take a look, please?
I changed property names. Have I missed something else?

oauth2/pom.xml Show resolved Hide resolved
Comment on lines 98 to 99
Authenticator.setDefault(new ProxyPasswordAuthentication(proxyProperties.getUsername(),
proxyProperties.getPassword().toCharArray()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't set the default authenticator, this needs to be scoped to this RestTemplate instance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +210 to +220
public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider validating the Proxy object above you have an if statement that both host and port either both required or both should be missing, but there is no validation, if one of them is missing

@@ -0,0 +1,108 @@
/*
* Copyright 2020-Present Okta, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2020-Present Okta, Inc.
* Copyright 2021-Present Okta, Inc.

Check other files added in this commit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Comment on lines 77 to 78
assertThat "Wrong user", Authenticator.theAuthenticator.getAt("proxyUser"), is("foo")
assertThat "Wrong password", Authenticator.theAuthenticator.getAt("proxyPassword").toString(), is("bar")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add the word proxy here


class OktaOAuth2AutoConfigRestTemplateTest {
private static final String LOCALHOST = "localhost"
private static final int PORT = 7000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use a static port for testing, use a random free port, this mockserver should support this out of the box, but if not, just lookup a free port first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

RestTemplate restTemplate = new OktaOAuth2AutoConfig().restTemplate(properties)
def headers = new HttpHeaders(singletonMap("Cookie", "sessionId=" + sessionId))
ResponseEntity<OAuth2AccessTokenResponse> response = restTemplate
.exchange("http://base_url.com", HttpMethod.GET, new HttpEntity<String>(headers), OAuth2AccessTokenResponse)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use example.com

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗

README.md Outdated
-Dhttps.proxyPort=port
-Dhttps.proxyUser="user" # optional
-Dhttps.proxyPassword="password". # optional
-Dokta.oauth2.proxy.host=host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add username and password properties here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗

README.md Outdated
```

or, you could set it programmatically like:

```java
System.setProperty("https.proxyHost", "https://example-proxy.com");
System.setProperty("https.proxyPort", "443");
System.setProperty("okta.oauth2.proxy.host", "example-proxy.com");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗

README.md Outdated
okta:
oauth2:
proxy:
host: "example-proxy.com"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use proxy.example.com (when possible use example.com as this is the intended use for this domain)

README.md Outdated Show resolved Hide resolved
README.md Outdated
Comment on lines 380 to 381
-Dokta.oauth2.proxy.username=username
-Dokta.oauth2.proxy.password=password
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-Dokta.oauth2.proxy.username=username
-Dokta.oauth2.proxy.password=password
-Dokta.oauth2.proxy.username=your-username # optional
-Dokta.oauth2.proxy.password=your-secret-password # optional

README.md Outdated
System.setProperty("okta.oauth2.proxy.host", "proxy.example.com");
System.setProperty("okta.oauth2.proxy.port", "7000");
System.setProperty("okta.oauth2.proxy.username", "username");
System.setProperty("okta.oauth2.proxy.password", "password");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above suggestion

@VitaliiTytarenko-okta
Copy link
Contributor Author

@bdemers @arvindkrishnakumar-okta
Done

Copy link
Contributor

@arvindkrishnakumar-okta arvindkrishnakumar-okta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm


OktaOAuth2Properties.Proxy proxyProperties = oktaOAuth2Properties.getProxy();
Optional<BasicAuthenticationInterceptor> basicAuthenticationInterceptor = Optional.empty();
if (proxyProperties != null && Strings.hasText(proxyProperties.getHost()) && proxyProperties.getPort() != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
if (proxyProperties != null && Strings.hasText(proxyProperties.getHost()) && proxyProperties.getPort() != 0) {
if (proxyProperties != null && Strings.hasText(proxyProperties.getHost()) && proxyProperties.getPort() > 0) {

NimbusJwtDecoder decoder = builder.build();
decoder.setJwtValidator(TokenUtil.jwtValidator(oktaOAuth2Properties.getIssuer(), oktaOAuth2Properties.getAudience()));
return decoder;
}

private RestTemplate restTemplate() {
private RestTemplate restTemplate(OktaOAuth2Properties oktaOAuth2Properties) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the duplicate code from: OktaOidcUserService

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

@bdemers bdemers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After removing the duplicate code, it looks like it's ready to go

@VitaliiTytarenko-okta VitaliiTytarenko-okta force-pushed the add-proxy_support branch 2 times, most recently from 143d403 to 9742f52 Compare February 4, 2021 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants