Skip to content

Commit

Permalink
Remove secrets passing to establish baseline for chapter 6 on app con…
Browse files Browse the repository at this point in the history
…fig.
  • Loading branch information
cdavisafc committed Feb 11, 2019
1 parent 484949c commit 8fc0d9d
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 268 deletions.
1 change: 0 additions & 1 deletion cloudnative-appconfig/Dockerfile
@@ -1,7 +1,6 @@
FROM openjdk:8-jdk-alpine
ARG jar_file
VOLUME /tmp
ADD VERSION .
ADD $jar_file app.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java -jar /app.jar
22 changes: 9 additions & 13 deletions cloudnative-appconfig/buildAndPushDockerImages.sh
@@ -1,19 +1,15 @@
docker build --build-arg \
jar_file=cloudnative-connectionposts/target/cloudnative-connectionposts-0.0.2-SNAPSHOT.jar \
-t cdavisafc/cloudnative-appconfig-connectionposts .
jar_file=cloudnative-connectionposts/target/cloudnative-connectionposts-0.0.1-SNAPSHOT.jar \
-t cdavisafc/cloudnative-appconfig-connectionposts:0.0.1 .
#
docker build --build-arg \
jar_file=cloudnative-connections/target/cloudnative-connections-0.0.2-SNAPSHOT.jar \
-t cdavisafc/cloudnative-appconfig-connections .
jar_file=cloudnative-connections/target/cloudnative-connections-0.0.1-SNAPSHOT.jar \
-t cdavisafc/cloudnative-appconfig-connections:0.0.1 .
#
docker build --build-arg \
jar_file=cloudnative-posts/target/cloudnative-posts-0.0.2-SNAPSHOT.jar \
-t cdavisafc/cloudnative-appconfig-posts .
jar_file=cloudnative-posts/target/cloudnative-posts-0.0.1-SNAPSHOT.jar \
-t cdavisafc/cloudnative-appconfig-posts:0.0.1 .
#
docker tag cdavisafc/cloudnative-appconfig-connectionposts cdavisafc/cloudnative-appconfig-connectionposts:0.0.2
docker tag cdavisafc/cloudnative-appconfig-connections cdavisafc/cloudnative-appconfig-connections:0.0.2
docker tag cdavisafc/cloudnative-appconfig-posts cdavisafc/cloudnative-appconfig-posts:0.0.2
#
docker push cdavisafc/cloudnative-appconfig-connectionposts:0.0.2
docker push cdavisafc/cloudnative-appconfig-connections:0.0.2
docker push cdavisafc/cloudnative-appconfig-posts:0.0.2
docker push cdavisafc/cloudnative-appconfig-connectionposts:0.0.1
docker push cdavisafc/cloudnative-appconfig-connections:0.0.1
docker push cdavisafc/cloudnative-appconfig-posts:0.0.1
6 changes: 1 addition & 5 deletions cloudnative-appconfig/cloudnative-connectionposts/pom.xml
Expand Up @@ -5,7 +5,7 @@

<groupId>com.corneliadavis</groupId>
<artifactId>cloudnative-connectionposts</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cloudnative-connectionposts</name>
Expand Down Expand Up @@ -66,10 +66,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

</dependencies>

Expand Down
Expand Up @@ -7,7 +7,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,7 +17,6 @@
import java.util.ArrayList;
import java.util.Date;

@RefreshScope
@RestController
public class ConnectionsPostsController {

Expand Down Expand Up @@ -72,10 +70,6 @@ public String getName() {
private String postsUrl;
@Value("${connectionpostscontroller.usersUrl}")
private String usersUrl;
@Value("${com.corneliadavis.cloudnative.posts.secrets}")
private String postsSecret;
@Value("${com.corneliadavis.cloudnative.connections.secrets}")
private String connectionsSecret;

private StringRedisTemplate template;

Expand All @@ -98,6 +92,7 @@ public Iterable<PostSummary> getByUsername(@CookieValue(value = "userToken", req
String username = ops.get(token);
if (username == null) {
logger.info(utils.ipTag() + "connectionsPosts access attempt with invalid token");

response.setStatus(401);
} else {

Expand All @@ -108,18 +103,16 @@ public Iterable<PostSummary> getByUsername(@CookieValue(value = "userToken", req
RestTemplate restTemplate = new RestTemplate();

// get connections
String secretQueryParam = "?secret=" + connectionsSecret;
ResponseEntity<ConnectionResult[]> respConns = restTemplate.getForEntity(connectionsUrl + username + secretQueryParam, ConnectionResult[].class);
ResponseEntity<ConnectionResult[]> respConns = restTemplate.getForEntity(connectionsUrl + username, ConnectionResult[].class);
ConnectionResult[] connections = respConns.getBody();
for (int i = 0; i < connections.length; i++) {
if (i > 0) ids += ",";
ids += connections[i].getFollowed().toString();
}
logger.info(utils.ipTag() + "connections = " + ids);

secretQueryParam = "&secret=" + postsSecret;
// get posts for those connections
ResponseEntity<PostResult[]> respPosts = restTemplate.getForEntity(postsUrl + ids + secretQueryParam, PostResult[].class);
ResponseEntity<PostResult[]> respPosts = restTemplate.getForEntity(postsUrl + ids, PostResult[].class);
PostResult[] posts = respPosts.getBody();

for (int i = 0; i < posts.length; i++)
Expand All @@ -133,8 +126,7 @@ public Iterable<PostSummary> getByUsername(@CookieValue(value = "userToken", req

private String getUsersname(Long id) {
RestTemplate restTemplate = new RestTemplate();
String secretQueryParam = "?secret=" + connectionsSecret;
ResponseEntity<UserResult> resp = restTemplate.getForEntity(usersUrl + id + secretQueryParam, UserResult.class);
ResponseEntity<UserResult> resp = restTemplate.getForEntity(usersUrl + id, UserResult.class);
return resp.getBody().getName();
}
}
Expand Up @@ -4,6 +4,4 @@ connectionpostscontroller.postsUrl=http://localhost:8081/posts?userIds=
connectionpostscontroller.usersUrl=http://localhost:8082/users/
ipaddress=${INSTANCE_IP:127.0.0.1}
redis.hostname=localhost
redis.port=6379
com.corneliadavis.cloudnative.posts.secrets=drawFromConfigServer
com.corneliadavis.cloudnative.connections.secrets=drawFromConfigServer
redis.port=6379
Expand Up @@ -28,10 +28,7 @@
@TestPropertySource(properties = {
"newfromconnectionscontroller.connectionsUrl:http://localhost:8080/connections/",
"newfromconnectionscontroller.postsUrl:http://localhost:8080/posts?userIds=",
"newfromconnectionscontroller.usersUrl:http://localhost:8080/users/",
"com.corneliadavis.cloudnative.posts.secrets:forTests",
"com.corneliadavis.cloudnative.connections.secrets:forTests",
"spring.cloud.config.enabled:false"})
"newfromconnectionscontroller.usersUrl:http://localhost:8080/users/"})
@AutoConfigureMockMvc
public class CloudnativeStatelessnessApplicationTests implements ApplicationContextAware {

Expand Down
6 changes: 1 addition & 5 deletions cloudnative-appconfig/cloudnative-connections/pom.xml
Expand Up @@ -5,7 +5,7 @@

<groupId>com.corneliadavis</groupId>
<artifactId>cloudnative-connections</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cloudnative-connections</name>
Expand Down Expand Up @@ -74,10 +74,6 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -22,9 +22,6 @@ public class RepositoriesPopulator implements ApplicationListener<ContextRefresh
private static final Logger logger = LoggerFactory.getLogger(RepositoriesPopulator.class);
private ApplicationContext applicationContext;

@Value("${com.corneliadavis.cloudnative.connections.secrets}")
private String secret;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Expand All @@ -44,18 +41,18 @@ private void populate() {
try {

user1 = new User("Cornelia", "cdavisafc");
connectionsWriteController.newUser(user1, secret, null);
connectionsWriteController.newUser(user1, null);
user2 = new User("Max", "madmax");
connectionsWriteController.newUser(user2, secret, null);
connectionsWriteController.newUser(user2, null);
user3 = new User("Glen", "gmaxdavis");
connectionsWriteController.newUser(user3, secret, null);
connectionsWriteController.newUser(user3, null);

connection1 = new Connection(2L, 1L);
connectionsWriteController.newConnection(connection1, secret, null);
connectionsWriteController.newConnection(connection1, null);
connection2 = new Connection(1L, 2L);
connectionsWriteController.newConnection(connection2, secret, null);
connectionsWriteController.newConnection(connection2, null);
connection3 = new Connection(1L, 3L);
connectionsWriteController.newConnection(connection3, secret, null);
connectionsWriteController.newConnection(connection3, null);

} catch (Exception e)
{
Expand Down

0 comments on commit 8fc0d9d

Please sign in to comment.