-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework platform headers to use our standard plugin method (#264)
Co-authored-by: Dean Hiller <[email protected]>
- Loading branch information
1 parent
5c59416
commit 9395f5a
Showing
39 changed files
with
508 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
cloud/cloud-api-lib/src/main/java/org/webpieces/microsvc/server/api/HeaderTranslation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.webpieces.microsvc.server.api; | ||
|
||
import org.webpieces.ctx.api.ClientServiceConfig; | ||
import org.webpieces.util.context.AddPlatformHeaders; | ||
import org.webpieces.util.context.Context; | ||
import org.webpieces.util.context.PlatformHeaders; | ||
|
||
import javax.inject.Inject; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class HeaderTranslation { | ||
|
||
private final List<PlatformHeaders> listHeaders; | ||
|
||
@Inject | ||
public HeaderTranslation( | ||
Set<AddPlatformHeaders> addPlatformHeaders, | ||
ClientServiceConfig config | ||
) { | ||
listHeaders = new ArrayList<>(); | ||
for(AddPlatformHeaders add : addPlatformHeaders) { | ||
Class<? extends PlatformHeaders> clazz = add.platformHeadersToAdd(); | ||
PlatformHeaders[] enumConstants = clazz.getEnumConstants(); | ||
List<PlatformHeaders> list = Arrays.asList(enumConstants); | ||
listHeaders.addAll(list); | ||
} | ||
|
||
Context.checkForDuplicates(listHeaders); | ||
} | ||
|
||
public List<PlatformHeaders> getHeaders() { | ||
return listHeaders; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...enerate-httpclient/src/main/java/org/webpieces/microsvc/client/api/AddGcpAuthHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.webpieces.microsvc.client.api; | ||
|
||
import org.webpieces.util.context.AddPlatformHeaders; | ||
import org.webpieces.util.context.PlatformHeaders; | ||
|
||
public class AddGcpAuthHeaders implements AddPlatformHeaders { | ||
@Override | ||
public Class<? extends PlatformHeaders> platformHeadersToAdd() { | ||
return GcpAuthHeader.class; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...d/generate-httpclient/src/main/java/org/webpieces/microsvc/client/api/Authentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.webpieces.microsvc.client.api; | ||
|
||
import org.webpieces.util.context.Context; | ||
|
||
public interface Authentication { | ||
public Object setupMagic(); | ||
|
||
public void resetMagic(Object state); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
cloud/generate-httpclient/src/main/java/org/webpieces/microsvc/client/api/GcpAuthHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.webpieces.microsvc.client.api; | ||
|
||
import org.webpieces.util.context.AddPlatformHeaders; | ||
import org.webpieces.util.context.PlatformHeaders; | ||
|
||
/** | ||
* | ||
*/ | ||
public enum GcpAuthHeader implements PlatformHeaders { | ||
|
||
AUTH_TOKEN("Authorization", null, false, true), | ||
METADATA_FLAVOR("Metadata-Flavor", null, false, false); | ||
|
||
private final String headerName; | ||
private final String logKey; | ||
private final boolean isLog; | ||
private final boolean isSecure; | ||
|
||
GcpAuthHeader(String headerName, String logKey, boolean isLog, boolean isSecure) { | ||
this.headerName = headerName; | ||
this.logKey = logKey; | ||
this.isLog = isLog; | ||
this.isSecure = isSecure; | ||
} | ||
|
||
|
||
@Override | ||
public String getHeaderName() { | ||
return headerName; | ||
} | ||
|
||
@Override | ||
public String getLoggerMDCKey() { | ||
return logKey; | ||
} | ||
|
||
@Override | ||
public boolean isWantLogged() { | ||
return isLog; | ||
} | ||
|
||
@Override | ||
public boolean isWantTransferred() { | ||
return headerName != null; | ||
} | ||
|
||
@Override | ||
public boolean isSecured() { | ||
return isSecure; | ||
} | ||
|
||
@Override | ||
public boolean isDimensionForMetrics() { | ||
return false; | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...enerate-httpclient/src/main/java/org/webpieces/microsvc/client/api/GcpAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.webpieces.microsvc.client.api; | ||
|
||
import org.webpieces.microsvc.client.impl.Endpoint; | ||
import org.webpieces.microsvc.client.impl.HttpsJsonClient; | ||
import org.webpieces.util.HostWithPort; | ||
import org.webpieces.util.SneakyThrow; | ||
import org.webpieces.util.context.Context; | ||
import org.webpieces.util.futures.XFuture; | ||
|
||
import javax.inject.Inject; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class GcpAuthentication implements Authentication { | ||
|
||
private final HttpsJsonClient jsonClient; | ||
private final Method method; | ||
private long expiresAtSeconds = 0; | ||
private String accessToken; | ||
|
||
@Inject | ||
public GcpAuthentication( | ||
HttpsJsonClient jsonClient | ||
) { | ||
this.jsonClient = jsonClient; | ||
|
||
try { | ||
method = GcpAuthentication.class.getDeclaredMethod("fetchToken"); | ||
} catch (NoSuchMethodException e) { | ||
throw SneakyThrow.sneak(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object setupMagic() { | ||
String previousSetting = Context.getMagic(GcpAuthHeader.AUTH_TOKEN); | ||
|
||
String accessToken = fetchToken(); | ||
Context.putMagic(GcpAuthHeader.AUTH_TOKEN, "Bearer "+accessToken); | ||
|
||
return previousSetting; | ||
} | ||
|
||
private synchronized String fetchToken() { | ||
long secondsEpochNow = System.currentTimeMillis() / 1000; | ||
if(secondsEpochNow < expiresAtSeconds) | ||
return accessToken; | ||
|
||
try { | ||
Context.putMagic(GcpAuthHeader.METADATA_FLAVOR, "Google"); | ||
HostWithPort host = new HostWithPort("metadata.google.internal", 80); | ||
Endpoint endpoint = new Endpoint(host, "GET", "/computeMetadata/v1/instance/service-accounts/default/token"); | ||
|
||
XFuture<TokenResponse> mapXFuture = jsonClient.sendHttpRequest(method, null, endpoint, TokenResponse.class, true); | ||
TokenResponse map = null; | ||
try { | ||
map = mapXFuture.get(20, TimeUnit.SECONDS); | ||
} catch (Exception e) { | ||
throw SneakyThrow.sneak(e); | ||
} | ||
accessToken = map.getAccessToken(); | ||
expiresAtSeconds = secondsEpochNow + map.getExpiresIn() - 30; //subtract 30 seconds so we renew before it expires | ||
|
||
return accessToken; | ||
} finally { | ||
Context.removeMagic(GcpAuthHeader.METADATA_FLAVOR); | ||
} | ||
} | ||
|
||
@Override | ||
public void resetMagic(Object state) { | ||
//restore to previous state or if state was null, remove magic.. | ||
if(state == null) { | ||
Context.removeMagic(GcpAuthHeader.AUTH_TOKEN); | ||
return; | ||
} | ||
|
||
String previousSetting = (String) state; | ||
Context.putMagic(GcpAuthHeader.AUTH_TOKEN, previousSetting); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
cloud/generate-httpclient/src/main/java/org/webpieces/microsvc/client/api/TokenResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.webpieces.microsvc.client.api; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class TokenResponse { | ||
/* | ||
{ | ||
"access_token":"ya29.AHES6ZRN3-HlhAPya30GnW_bHSb_QtAi85nHq39HE3C2LTrCARA", | ||
"expires_in":3599, | ||
"token_type":"Bearer" | ||
} | ||
*/ | ||
|
||
@JsonProperty("access_token") | ||
private String accessToken; | ||
@JsonProperty("expires_in") | ||
private Long expiresIn; | ||
@JsonProperty("token_type") | ||
private String tokenType; | ||
|
||
public String getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public void setAccessToken(String accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
|
||
public Long getExpiresIn() { | ||
return expiresIn; | ||
} | ||
|
||
public void setExpiresIn(Long expiresIn) { | ||
this.expiresIn = expiresIn; | ||
} | ||
|
||
public String getTokenType() { | ||
return tokenType; | ||
} | ||
|
||
public void setTokenType(String tokenType) { | ||
this.tokenType = tokenType; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
cloud/generate-httpclient/src/main/java/org/webpieces/microsvc/client/impl/EmptyAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.webpieces.microsvc.client.impl; | ||
|
||
import org.webpieces.microsvc.client.api.Authentication; | ||
|
||
public class EmptyAuth implements Authentication { | ||
@Override | ||
public Object setupMagic() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void resetMagic(Object state) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.