Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Initial code for kubernetes integration (#75)
Browse files Browse the repository at this point in the history
* Initial code for kubernetes integration

fixes #61

* drop jdk-7

* first review changes

* rename to FilesystemSecrets****

* rename package 'k8s'->'fs'

* handle 'clients' and 'authorizations'

* update to latest version of mockito and assertj, remove config for

javadoc-maven-plugin (now taken from parent-pom), fix tests according
these changes, replace 'Clients','Authorizations' with 'Secrets'

* do not use deprecated methods in tests

* fix 'resource-leak' warning

* minor updates in README

* [maven-release-plugin] prepare release 0.11.0-beta-1

* [maven-release-plugin] prepare for next development iteration

* remove any usage of 'k8s', 'kube' or 'kubernetes'

* [maven-release-plugin] prepare release 0.11.0-beta-2

* [maven-release-plugin] prepare for next development iteration
  • Loading branch information
jbellmann authored and hjacobs committed Mar 8, 2017
1 parent d7e70c8 commit 0d0ba26
Show file tree
Hide file tree
Showing 37 changed files with 1,142 additions and 147 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: java

jdk:
- oraclejdk8
- oraclejdk7

before_script:
- pip install --user codecov
Expand Down
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ When creating tokens, it's easy to make a lot of mistakes. Tokens aims to save y

### Prerequisites

- Java 8
- Maven
- [Apache-HttpClient](https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient)
- Gradle

#### Maven Dependency

Expand All @@ -38,17 +39,9 @@ Add it with:
</dependency>
```

####Apache-HttpClient
#### Gradle Dependency

Add it with:

```xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>your version</version>
</dependency>
```
``compile('org.zalando.stups:tokens:${version}') ``

### Usage

Expand Down
1 change: 1 addition & 0 deletions fs/kio-client-id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9474746353524
1 change: 1 addition & 0 deletions fs/kio-client-secret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
94747463535249474746353524947474635352494747463535249474746353524
1 change: 1 addition & 0 deletions fs/mybasic-token-secret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
geheim
1 change: 1 addition & 0 deletions fs/mybasic-token-type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basic
1 change: 1 addition & 0 deletions fs/myfirst-token-secret
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
987654321123456789
1 change: 1 addition & 0 deletions fs/myfirst-token-type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bearer
28 changes: 7 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>org.zalando.stups</groupId>
<artifactId>tokens</artifactId>
<version>0.10.1-SNAPSHOT</version>
<version>0.11.0-SNAPSHOT</version>

<name>Tokens</name>
<description>
Expand Down Expand Up @@ -45,8 +45,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.source>1.7</java.source>
<java.target>1.7</java.target>
<java.source>1.8</java.source>
<java.target>1.8</java.target>

<coveralls.dryRun>true</coveralls.dryRun>
<main.basedir>${basedir}</main.basedir>
Expand Down Expand Up @@ -96,14 +96,14 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<version>2.7.14</version>
<scope>test</scope>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.0.0</version>
<version>3.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -154,26 +154,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<version>3.6.1</version>
<configuration>
<source>${java.source}</source>
<target>${java.target}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<configuration combine.self="override">
<breakiterator>true</breakiterator>
<header>${project.name}</header>
<source>${source.level}</source>
<quiet>true</quiet>
<links>
<link>http://docs.oracle.com/javase/7/docs/api</link>
</links>
<additionalparam />
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.zalando.stups.tokens;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractAccessTokenRefresher implements AccessTokens {

private static final Logger LOG = LoggerFactory.getLogger(AbstractAccessTokenRefresher.class);

private static final long ONE_YEAR_SECONDS = TimeUnit.DAYS.toSeconds(365);
private static final String FIXED_TOKENS_ENV_VAR = "OAUTH2_ACCESS_TOKENS";

protected final TokenRefresherConfiguration configuration;
protected final ScheduledExecutorService scheduler;

protected final ConcurrentHashMap<Object, AccessToken> accessTokens = new ConcurrentHashMap<>();

public AbstractAccessTokenRefresher(TokenRefresherConfiguration configuration) {
this.configuration = configuration;
this.scheduler = configuration.getExecutorService();
}

protected void initializeFixedTokensFromEnvironment() {
final String csv = getFixedToken();
if (csv != null) {
LOG.info("Initializing fixed access tokens from {} environment variable..", FIXED_TOKENS_ENV_VAR);

final String[] tokens = csv.split(",");
final long expiresInSeconds = ONE_YEAR_SECONDS;
final Date validUntil = new Date(System.currentTimeMillis() + (expiresInSeconds * 1000));
for (String token : tokens) {
final String[] keyValue = token.split("=");
if (keyValue.length == 2) {
LOG.info("Using fixed access token {}..", keyValue[0]);
accessTokens.put(keyValue[0], new AccessToken(keyValue[1], "fixed", expiresInSeconds, validUntil));
} else {
LOG.error("Could not create access token from {}", token);
}
}
}
}

public abstract void start();

protected String getFixedToken() {
final String tokens = System.getProperty(FIXED_TOKENS_ENV_VAR);
if (tokens == null) {
return System.getenv(FIXED_TOKENS_ENV_VAR);
}

return tokens;
}

@Override
public String get(final Object tokenId) throws AccessTokenUnavailableException {
return getAccessToken(tokenId).getToken();
}

@Override
public AccessToken getAccessToken(final Object tokenId) throws AccessTokenUnavailableException {
final AccessToken token = accessTokens.get(tokenId);
if (token == null) {
throw new AccessTokenUnavailableException("no token available");
}

if (token.isExpired()) {
throw new AccessTokenUnavailableException("token expired");
}

return token;
}

@Override
public void invalidate(final Object tokenId) {
accessTokens.remove(tokenId);
}

@Override
public void stop() {
scheduler.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.zalando.stups.tokens;

import static org.zalando.stups.tokens.util.Objects.noNullEntries;
import static org.zalando.stups.tokens.util.Objects.noBlankEntries;
import static org.zalando.stups.tokens.util.Objects.noNullEntries;
import static org.zalando.stups.tokens.util.Objects.notBlank;
import static org.zalando.stups.tokens.util.Objects.notNull;

Expand Down Expand Up @@ -160,7 +160,7 @@ public AccessTokenConfiguration withGrantType(final String grantType) {
return this;
}

Object getTokenId() {
public Object getTokenId() {
return tokenId;
}

Expand Down
85 changes: 10 additions & 75 deletions src/main/java/org/zalando/stups/tokens/AccessTokenRefresher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,76 +17,37 @@

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zalando.stups.tokens.mcb.MCB;
import org.zalando.stups.tokens.util.Metrics;
import org.zalando.stups.tokens.util.Objects;

class AccessTokenRefresher implements AccessTokens, Runnable {
class AccessTokenRefresher extends AbstractAccessTokenRefresher implements AccessTokens, Runnable {
private static final Logger LOG = LoggerFactory.getLogger(AccessTokenRefresher.class);

private static final long ONE_YEAR_SECONDS = TimeUnit.DAYS.toSeconds(365);
private static final String FIXED_TOKENS_ENV_VAR = "OAUTH2_ACCESS_TOKENS";
private static final String METRICS_KEY_PREFIX = "tokens.refresher";

private final TokenRefresherConfiguration configuration;
private final ScheduledExecutorService scheduler;

private final MCB mcb;

private final ConcurrentHashMap<Object, AccessToken> accessTokens = new ConcurrentHashMap<>();
private final Set<Object> invalidTokens = Collections.newSetFromMap(new ConcurrentHashMap<Object, Boolean>());

private final TokenVerifyRunner verifyRunner;

private final MetricsListener metricsListener;

public AccessTokenRefresher(final TokenRefresherConfiguration configuration) {
this.configuration = configuration;
this.scheduler = configuration.getExecutorService();
super(configuration);
this.metricsListener = configuration.getMetricsListener();
this.verifyRunner = new TokenVerifyRunner(configuration, accessTokens, invalidTokens);
this.mcb = new MCB(this.configuration.getTokenRefresherMcbConfig());
}

protected void initializeFixedTokensFromEnvironment() {
final String csv = getFixedToken();
if (csv != null) {
LOG.info("Initializing fixed access tokens from {} environment variable..", FIXED_TOKENS_ENV_VAR);

final String[] tokens = csv.split(",");
final long expiresInSeconds = ONE_YEAR_SECONDS;
final Date validUntil = new Date(System.currentTimeMillis() + (expiresInSeconds * 1000));
for (String token : tokens) {
final String[] keyValue = token.split("=");
if (keyValue.length == 2) {
LOG.info("Using fixed access token {}..", keyValue[0]);
accessTokens.put(keyValue[0], new AccessToken(keyValue[1], "fixed", expiresInSeconds, validUntil));
} else {
LOG.error("Could not create access token from {}", token);
}
}
}
}

// visible for testing
protected String getFixedToken() {
final String tokens = System.getProperty(FIXED_TOKENS_ENV_VAR);
if (tokens == null) {
return System.getenv(FIXED_TOKENS_ENV_VAR);
}

return tokens;
this.mcb = new MCB(configuration.getTokenRefresherMcbConfig());
}

void start() {
@Override
public void start() {
initializeFixedTokensFromEnvironment();
LOG.info("Starting to refresh tokens regularly...");
run();
Expand Down Expand Up @@ -182,37 +143,11 @@ private AccessToken createToken(final AccessTokenConfiguration tokenConfig) {
}
}

private HttpProvider buildHttpProvider(ClientCredentials clientCredentials, UserCredentials userCredentials){
return configuration.getHttpProviderFactory().create(clientCredentials,
userCredentials, configuration.getAccessTokenUri(), configuration.getHttpConfig());
}
private HttpProvider buildHttpProvider(ClientCredentials clientCredentials, UserCredentials userCredentials) {
HttpProviderFactory providerFactory = configuration.getHttpProviderFactory();
HttpProvider httpProvider = providerFactory.create(clientCredentials, userCredentials,
configuration.getAccessTokenUri(), configuration.getHttpConfig());

@Override
public String get(final Object tokenId) throws AccessTokenUnavailableException {
return getAccessToken(tokenId).getToken();
}

@Override
public AccessToken getAccessToken(final Object tokenId) throws AccessTokenUnavailableException {
final AccessToken token = accessTokens.get(tokenId);
if (token == null) {
throw new AccessTokenUnavailableException("no token available");
}

if (token.isExpired()) {
throw new AccessTokenUnavailableException("token expired");
}

return token;
}

@Override
public void invalidate(final Object tokenId) {
accessTokens.remove(tokenId);
}

@Override
public void stop() {
scheduler.shutdown();
return httpProvider;
}
}
Loading

0 comments on commit 0d0ba26

Please sign in to comment.