From 10f3d81b89dfe6c83831aff8a004d19747b51492 Mon Sep 17 00:00:00 2001 From: Joerg Bellmann Date: Tue, 29 Sep 2015 12:33:50 +0200 Subject: [PATCH] expose ClientCredentialsProvider --- .../AccessTokensBeanAutoConfiguration.java | 18 +++- .../config/AccessTokensBeanProperties.java | 19 ++--- .../pkg/ClientCredentialsProviderIT.java | 84 +++++++++++++++++++ .../pkg/DoNotClientCredentialsProviderIT.java | 69 +++++++++++++++ .../config/application-exposeClient.yml | 20 +++++ .../config/application-notExposeClient.yml | 19 +++++ 6 files changed, 217 insertions(+), 12 deletions(-) create mode 100644 spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/ClientCredentialsProviderIT.java create mode 100644 spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/DoNotClientCredentialsProviderIT.java create mode 100644 spring-boot-zalando-stups-tokens/src/test/resources/config/application-exposeClient.yml create mode 100644 spring-boot-zalando-stups-tokens/src/test/resources/config/application-notExposeClient.yml diff --git a/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanAutoConfiguration.java b/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanAutoConfiguration.java index 5fae142..9f6a903 100644 --- a/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanAutoConfiguration.java +++ b/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanAutoConfiguration.java @@ -15,14 +15,18 @@ */ package org.zalando.stups.tokens.config; -import org.springframework.beans.factory.annotation.Autowired; +import java.io.File; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.zalando.stups.tokens.AccessTokensBean; +import org.zalando.stups.tokens.ClientCredentialsProvider; +import org.zalando.stups.tokens.JsonFileBackedClientCredentialsProvider; /** * @author jbellmann @@ -38,4 +42,16 @@ public class AccessTokensBeanAutoConfiguration { public AccessTokensBean accessTokensBean() { return new AccessTokensBean(accessTokensBeanProperties); } + + @Bean + @ConditionalOnProperty(prefix="tokens", name="exposeClientCredentialProvider", havingValue="true") + public ClientCredentialsProvider clientCredentialsProvider(){ + return new JsonFileBackedClientCredentialsProvider(getCredentialsFile( + accessTokensBeanProperties.getClientCredentialsFilename())); + } + + protected File getCredentialsFile(final String credentialsFilename) { + return new File(accessTokensBeanProperties.getCredentialsDirectory(), credentialsFilename); + } + } diff --git a/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanProperties.java b/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanProperties.java index ac3cc9c..44bf00b 100644 --- a/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanProperties.java +++ b/spring-boot-zalando-stups-tokens/src/main/java/org/zalando/stups/tokens/config/AccessTokensBeanProperties.java @@ -49,6 +49,8 @@ public class AccessTokensBeanProperties { private boolean autoStartup = true; private int phase = 0; + + private boolean exposeClientCredentialProvider = false; private List tokenConfigurationList = new ArrayList(0); @@ -88,18 +90,10 @@ public List getTokenConfigurationList() { return tokenConfigurationList; } -// public void setTokenConfigurationList(final List tokenConfigurationList) { -// this.tokenConfigurationList = tokenConfigurationList; -// } - public String getUserCredentialsFilename() { return userCredentialsFilename; } -// public void setUserCredentialsFilename(final String userCredentialsFilename) { -// this.userCredentialsFilename = userCredentialsFilename; -// } - public String getClientCredentialsFilename() { return clientCredentialsFilename; } @@ -128,8 +122,11 @@ public void setPhase(final int phase) { this.phase = phase; } -// public void setClientCredentialsFilename(final String clientCredentialsFilename) { -// this.clientCredentialsFilename = clientCredentialsFilename; -// } + public boolean isExposeClientCredentialProvider() { + return exposeClientCredentialProvider; + } + public void setExposeClientCredentialProvider(boolean exposeClientCredentialProvider) { + this.exposeClientCredentialProvider = exposeClientCredentialProvider; + } } diff --git a/spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/ClientCredentialsProviderIT.java b/spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/ClientCredentialsProviderIT.java new file mode 100644 index 0000000..c4fa07b --- /dev/null +++ b/spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/ClientCredentialsProviderIT.java @@ -0,0 +1,84 @@ +/** + * 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 com.unknown.pkg; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.assertj.core.api.Assertions; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; + +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import org.zalando.stups.tokens.AccessToken; +import org.zalando.stups.tokens.AccessTokens; +import org.zalando.stups.tokens.AccessTokensBean; +import org.zalando.stups.tokens.ClientCredentialsProvider; +import org.zalando.stups.tokens.config.AccessTokensBeanProperties; +import org.zalando.stups.tokens.config.TokenConfiguration; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; + +/** + * @author jbellmann + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {TokenTestApplication.class}) +@WebIntegrationTest(randomPort = false) +@ActiveProfiles("exposeClient") +public class ClientCredentialsProviderIT { + + static final String OAUTH2_ACCESS_TOKENS = "OAUTH2_ACCESS_TOKENS"; + + @Autowired + private AccessTokensBean tokens; + + @Autowired + private AccessTokensBeanProperties accessTokensBeanProperties; + + @Autowired + private AccessTokens accessTokens; + + @Autowired + private ClientCredentialsProvider clientCredentialsProvider; + + @BeforeClass + public static void setUp() { + System.getProperties().remove(OAUTH2_ACCESS_TOKENS); + } + + @Test + public void testClientCredentialProviderIsPresent() throws InterruptedException { + Assertions.assertThat(clientCredentialsProvider).isNotNull(); + String clientId = clientCredentialsProvider.get().getId(); + Assertions.assertThat(clientId).isNotNull(); + Assertions.assertThat(clientId).isEqualTo("foo"); + String clientSecret = clientCredentialsProvider.get().getSecret(); + Assertions.assertThat(clientSecret).isNotNull(); + Assertions.assertThat(clientSecret).isEqualTo("bar"); + } +} diff --git a/spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/DoNotClientCredentialsProviderIT.java b/spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/DoNotClientCredentialsProviderIT.java new file mode 100644 index 0000000..427c931 --- /dev/null +++ b/spring-boot-zalando-stups-tokens/src/test/java/com/unknown/pkg/DoNotClientCredentialsProviderIT.java @@ -0,0 +1,69 @@ +/** + * 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 com.unknown.pkg; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.assertj.core.api.Assertions; + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; + +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import org.zalando.stups.tokens.AccessToken; +import org.zalando.stups.tokens.AccessTokens; +import org.zalando.stups.tokens.AccessTokensBean; +import org.zalando.stups.tokens.ClientCredentialsProvider; +import org.zalando.stups.tokens.config.AccessTokensBeanProperties; +import org.zalando.stups.tokens.config.TokenConfiguration; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; + +/** + * @author jbellmann + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {TokenTestApplication.class}) +@WebIntegrationTest(randomPort = false) +@ActiveProfiles("notExposeClient") +public class DoNotClientCredentialsProviderIT { + + static final String OAUTH2_ACCESS_TOKENS = "OAUTH2_ACCESS_TOKENS"; + + @Autowired(required=false) + private ClientCredentialsProvider clientCredentialsProvider; + + @BeforeClass + public static void setUp() { + System.getProperties().remove(OAUTH2_ACCESS_TOKENS); + } + + @Test + public void testClientCredentialProviderIsPresent() throws InterruptedException { + Assertions.assertThat(clientCredentialsProvider).isNull(); + } +} diff --git a/spring-boot-zalando-stups-tokens/src/test/resources/config/application-exposeClient.yml b/spring-boot-zalando-stups-tokens/src/test/resources/config/application-exposeClient.yml new file mode 100644 index 0000000..57f834d --- /dev/null +++ b/spring-boot-zalando-stups-tokens/src/test/resources/config/application-exposeClient.yml @@ -0,0 +1,20 @@ +server: + port: 9192 +# +# +tokens: + accessTokenUri: http://localhost:9191/access_token?realm=whatever + credentialsDirectory: ${user.dir}/somepath/credentials + refreshPercentLeft: 30 + warnPercentLeft: 10 + autoStartup: true + exposeClientCredentialProvider: true + + token-configuration-list: + - tokenId: firstService + scopes: + - refole:read + - refole:write + - refole:all + - tokenId: secondService + scopes: singleScope:all \ No newline at end of file diff --git a/spring-boot-zalando-stups-tokens/src/test/resources/config/application-notExposeClient.yml b/spring-boot-zalando-stups-tokens/src/test/resources/config/application-notExposeClient.yml new file mode 100644 index 0000000..f6b6a38 --- /dev/null +++ b/spring-boot-zalando-stups-tokens/src/test/resources/config/application-notExposeClient.yml @@ -0,0 +1,19 @@ +server: + port: 9193 +# +# +tokens: + accessTokenUri: http://localhost:9191/access_token?realm=whatever + credentialsDirectory: ${user.dir}/somepath/credentials + refreshPercentLeft: 30 + warnPercentLeft: 10 + autoStartup: true + + token-configuration-list: + - tokenId: firstService + scopes: + - refole:read + - refole:write + - refole:all + - tokenId: secondService + scopes: singleScope:all \ No newline at end of file