Skip to content

Commit 1833245

Browse files
mhmxsbbihari
authored andcommitted
CLOUD-86088 Encrypt sensitive data in periscope database
1 parent c8815fb commit 1833245

File tree

13 files changed

+123
-38
lines changed

13 files changed

+123
-38
lines changed

autoscale/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ dependencies {
5555
compile group: 'dnsjava', name: 'dnsjava', version: '2.1.7'
5656
compile group: 'org.mybatis', name: 'mybatis-migrations', version: '3.2.0'
5757

58-
5958
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.5.0'
6059
compile group: 'io.springfox', name: 'springfox-core', version: '2.5.0'
6160
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.5.0'

autoscale/src/main/java/com/sequenceiq/periscope/config/SecurityConfig.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
import javax.inject.Inject;
66

7+
import org.jasypt.encryption.pbe.PBEStringCleanablePasswordEncryptor;
8+
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
711
import org.springframework.context.annotation.Configuration;
812
import org.springframework.context.annotation.Lazy;
13+
import org.springframework.context.annotation.Scope;
914
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
1015
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
1116
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;
@@ -23,6 +28,25 @@
2328
@Configuration
2429
public class SecurityConfig {
2530

31+
@Value("${periscope.client.secret}")
32+
private String clientSecret;
33+
34+
@Bean("PBEStringCleanablePasswordEncryptor")
35+
@Scope("prototype")
36+
public PBEStringCleanablePasswordEncryptor encryptor() {
37+
PBEStringCleanablePasswordEncryptor encryptor = new StandardPBEStringEncryptor();
38+
encryptor.setPassword(clientSecret);
39+
return encryptor;
40+
}
41+
42+
@Bean("LegacyPBEStringCleanablePasswordEncryptor")
43+
@Scope("prototype")
44+
public PBEStringCleanablePasswordEncryptor legacyEncryptor() {
45+
PBEStringCleanablePasswordEncryptor encryptor = new StandardPBEStringEncryptor();
46+
encryptor.setPassword("cbsecret2015");
47+
return encryptor;
48+
}
49+
2650
@EnableGlobalMethodSecurity(prePostEnabled = true)
2751
protected static class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
2852

@@ -49,7 +73,7 @@ protected static class ResourceServerConfiguration extends ResourceServerConfigu
4973
private ResourceServerTokenServices resourceServerTokenServices;
5074

5175
@Override
52-
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
76+
public void configure(ResourceServerSecurityConfigurer resources) {
5377
resources.resourceId("periscope");
5478
resources.tokenServices(resourceServerTokenServices);
5579
}

autoscale/src/main/java/com/sequenceiq/periscope/controller/EndpointConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
@ApplicationPath(AutoscaleApi.API_ROOT_CONTEXT)
3434
@Component
35-
public class EndpointConfig extends ResourceConfig {
35+
public class EndpointConfig extends ResourceConfig {
3636

3737
public EndpointConfig() throws IOException {
3838
registerEndpoints();

autoscale/src/main/java/com/sequenceiq/periscope/domain/Ambari.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import javax.persistence.Id;
88
import javax.persistence.SequenceGenerator;
99

10+
import org.hibernate.annotations.Type;
11+
1012
@Entity
1113
public class Ambari {
1214

@@ -21,9 +23,11 @@ public class Ambari {
2123
@Column(name = "ambari_port")
2224
private String port;
2325

26+
@Type(type = "encrypted_string")
2427
@Column(name = "ambari_user")
2528
private String user;
2629

30+
@Type(type = "encrypted_string")
2731
@Column(name = "ambari_pass")
2832
private String pass;
2933

autoscale/src/main/java/com/sequenceiq/periscope/domain/SecurityConfig.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import javax.persistence.Table;
1313

1414
import org.apache.commons.codec.binary.Base64;
15+
import org.hibernate.annotations.Type;
1516

1617
@Entity
1718
@Table(name = "SecurityConfig")
@@ -23,22 +24,25 @@ public class SecurityConfig {
2324
@SequenceGenerator(name = "securityconfig_generator", sequenceName = "securityconfig_id_seq", allocationSize = 1)
2425
private Long id;
2526

26-
@Column
27-
private byte[] clientKey;
27+
@Type(type = "encrypted_string")
28+
@Column(columnDefinition = "TEXT")
29+
private String clientKey;
2830

29-
@Column
30-
private byte[] clientCert;
31+
@Type(type = "encrypted_string")
32+
@Column(columnDefinition = "TEXT")
33+
private String clientCert;
3134

32-
@Column
33-
private byte[] serverCert;
35+
@Type(type = "encrypted_string")
36+
@Column(columnDefinition = "TEXT")
37+
private String serverCert;
3438

3539
@OneToOne
3640
private Cluster cluster;
3741

3842
public SecurityConfig() {
3943
}
4044

41-
public SecurityConfig(byte[] clientKey, byte[] clientCert, byte[] serverCert) {
45+
public SecurityConfig(String clientKey, String clientCert, String serverCert) {
4246
this.clientKey = clientKey;
4347
this.clientCert = clientCert;
4448
this.serverCert = serverCert;
@@ -72,27 +76,27 @@ public void setCluster(Cluster cluster) {
7276
this.cluster = cluster;
7377
}
7478

75-
public byte[] getClientKey() {
79+
public String getClientKey() {
7680
return clientKey;
7781
}
7882

79-
public byte[] getClientCert() {
83+
public String getClientCert() {
8084
return clientCert;
8185
}
8286

83-
public byte[] getServerCert() {
87+
public String getServerCert() {
8488
return serverCert;
8589
}
8690

87-
public void setClientKey(byte[] clientKey) {
91+
public void setClientKey(String clientKey) {
8892
this.clientKey = clientKey;
8993
}
9094

91-
public void setClientCert(byte[] clientCert) {
95+
public void setClientCert(String clientCert) {
9296
this.clientCert = clientCert;
9397
}
9498

95-
public void setServerCert(byte[] serverCert) {
99+
public void setServerCert(String serverCert) {
96100
this.serverCert = serverCert;
97101
}
98102

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@TypeDefs(@TypeDef(name = "encrypted_string", typeClass = EncryptedStringType.class, parameters = @Parameter(name = "encryptorRegisteredName",
2+
value = "hibernateStringEncryptor")))
3+
4+
package com.sequenceiq.periscope.domain;
5+
6+
import org.hibernate.annotations.Parameter;
7+
import org.hibernate.annotations.TypeDef;
8+
import org.hibernate.annotations.TypeDefs;
9+
import org.jasypt.hibernate4.type.EncryptedStringType;

autoscale/src/main/java/com/sequenceiq/periscope/service/registry/RetryingServiceAddressResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private void handleException(ServiceAddressResolvingException e, int attemptCoun
5858
LOGGER.warn("Unsuccessful address resolving: {}, retrying in {}millis", e.getMessage(), SLEEPTIME);
5959
Thread.sleep(SLEEPTIME);
6060
} catch (InterruptedException ie) {
61-
LOGGER.warn("Interrupted exception occurred.", ie.getMessage());
61+
LOGGER.warn("Interrupted exception occurred: {}", ie.getMessage());
6262
}
6363
}
6464
}

autoscale/src/main/java/com/sequenceiq/periscope/service/security/TlsSecurityService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public SecurityConfig prepareSecurityConfig(Long stackId) {
2626
byte[] serverCert = Base64.encode(response.getServerCert());
2727
byte[] clientKey = Base64.encode(response.getClientKey());
2828
byte[] clientCert = Base64.encode(response.getClientCert());
29-
return new SecurityConfig(clientKey, clientCert, serverCert);
29+
return new SecurityConfig(new String(clientKey), new String(clientCert), new String(serverCert));
3030
}
3131

3232
public TlsConfiguration getConfiguration(Cluster cluster) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- // CLOUD-86088_encrypt_database_fields
2+
-- Migration SQL that makes the change goes here.
3+
4+
CREATE TABLE securityconfig_encrypted (
5+
id bigint NOT NULL,
6+
clientkey text,
7+
clientcert text,
8+
servercert text,
9+
cluster_id bigint NOT NULL
10+
);
11+
12+
ALTER TABLE securityconfig_encrypted ALTER COLUMN id SET DEFAULT nextval ('securityconfig_id_seq');
13+
14+
INSERT INTO securityconfig_encrypted (clientkey, clientcert, servercert, cluster_id) SELECT encode(clientkey, 'escape'), encode(clientcert, 'escape'), encode(servercert, 'escape'), cluster_id FROM securityconfig;
15+
16+
DROP TABLE securityconfig;
17+
18+
ALTER TABLE securityconfig_encrypted RENAME TO securityconfig;
19+
20+
ALTER TABLE ONLY securityconfig ADD CONSTRAINT securityconfig_pkey PRIMARY KEY (id);
21+
22+
ALTER TABLE ONLY securityconfig ADD CONSTRAINT fk_securityconfig_cluster_id FOREIGN KEY (cluster_id) REFERENCES cluster(id);
23+
24+
-- //@UNDO
25+
-- SQL to undo the change goes here.
26+
27+
CREATE TABLE securityconfig_decrypted (
28+
id bigint NOT NULL,
29+
clientkey bytea,
30+
clientcert bytea,
31+
servercert bytea,
32+
cluster_id bigint NOT NULL
33+
);
34+
35+
ALTER TABLE securityconfig_decrypted ALTER COLUMN id SET DEFAULT nextval ('securityconfig_id_seq');
36+
37+
INSERT INTO securityconfig_decrypted (clientkey, clientcert, servercert, cluster_id) SELECT decode(clientkey, 'escape'), decode(clientcert, 'escape'), decode(servercert, 'escape'), cluster_id FROM securityconfig;
38+
39+
DROP TABLE securityconfig;
40+
41+
ALTER TABLE securityconfig_decrypted RENAME TO securityconfig;
42+
43+
ALTER TABLE ONLY securityconfig ADD CONSTRAINT securityconfig_pkey PRIMARY KEY (id);
44+
45+
ALTER TABLE ONLY securityconfig ADD CONSTRAINT fk_securityconfig_cluster_id FOREIGN KEY (cluster_id) REFERENCES cluster(id);

cloud-common/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies {
4040
compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212'
4141
compile group: 'com.sequenceiq', name: 'consul-api', version: '1.10'
4242
compile group: 'org.apache.commons', name: 'commons-lang3', version: apacheCommonsLangVersion
43+
compile group: 'org.jasypt', name: 'jasypt-hibernate4', version: jasyptVersion
4344
testCompile group: 'junit', name: 'junit', version: junitVersion
4445
testCompile group: 'org.mockito', name: 'mockito-all', version: mockitoAllVersion
4546
}

0 commit comments

Comments
 (0)