Skip to content

Commit 46696bd

Browse files
dineshchitlangiabharatviswa504
authored andcommitted
HDDS-2014. Create Symmetric Key for GDPR (#1362)
1 parent ec34cee commit 46696bd

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConsts.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,13 @@ private OzoneConsts() {
312312
public static final int S3_BUCKET_MIN_LENGTH = 3;
313313
public static final int S3_BUCKET_MAX_LENGTH = 64;
314314

315+
//GDPR
316+
public static final String GDPR_ALGORITHM_NAME = "AES";
317+
public static final int GDPR_RANDOM_SECRET_LENGTH = 32;
318+
public static final String GDPR_CHARSET = "UTF-8";
319+
public static final String GDPR_LENGTH = "length";
320+
public static final String GDPR_SECRET = "secret";
321+
public static final String GDPR_ALGORITHM = "algorithm";
322+
323+
315324
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
package org.apache.hadoop.ozone.security;
18+
19+
import com.google.common.base.Preconditions;
20+
import org.apache.commons.lang3.RandomStringUtils;
21+
import org.apache.hadoop.ozone.OzoneConsts;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import javax.crypto.Cipher;
27+
import javax.crypto.spec.SecretKeySpec;
28+
29+
/**
30+
* Symmetric Key structure for GDPR.
31+
*/
32+
public class GDPRSymmetricKey {
33+
34+
private SecretKeySpec secretKey;
35+
private Cipher cipher;
36+
private String algorithm;
37+
private String secret;
38+
39+
public SecretKeySpec getSecretKey() {
40+
return secretKey;
41+
}
42+
43+
public Cipher getCipher() {
44+
return cipher;
45+
}
46+
47+
/**
48+
* Default constructor creates key with default values.
49+
* @throws Exception
50+
*/
51+
public GDPRSymmetricKey() throws Exception {
52+
algorithm = OzoneConsts.GDPR_ALGORITHM_NAME;
53+
secret = RandomStringUtils
54+
.randomAlphabetic(OzoneConsts.GDPR_RANDOM_SECRET_LENGTH);
55+
this.secretKey = new SecretKeySpec(
56+
secret.getBytes(OzoneConsts.GDPR_CHARSET), algorithm);
57+
this.cipher = Cipher.getInstance(algorithm);
58+
}
59+
60+
/**
61+
* Overloaded constructor creates key with specified values.
62+
* @throws Exception
63+
*/
64+
public GDPRSymmetricKey(String secret, String algorithm) throws Exception {
65+
Preconditions.checkArgument(secret.length() == 32,
66+
"Secret must be exactly 32 characters");
67+
this.secret = secret;
68+
this.algorithm = algorithm;
69+
this.secretKey = new SecretKeySpec(
70+
secret.getBytes(OzoneConsts.GDPR_CHARSET), algorithm);
71+
this.cipher = Cipher.getInstance(algorithm);
72+
}
73+
74+
public Map<String, String> getKeyDetails() {
75+
Map<String, String> keyDetail = new HashMap<>();
76+
keyDetail.put(OzoneConsts.GDPR_SECRET, this.secret);
77+
keyDetail.put(OzoneConsts.GDPR_ALGORITHM, this.algorithm);
78+
return keyDetail;
79+
}
80+
81+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
package org.apache.hadoop.ozone.security;
18+
19+
import org.apache.hadoop.ozone.OzoneConsts;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/**
24+
* Tests GDPRSymmetricKey structure.
25+
*/
26+
public class TestGDPRSymmetricKey {
27+
28+
@Test
29+
public void testKeyGenerationWithDefaults() throws Exception {
30+
GDPRSymmetricKey gkey = new GDPRSymmetricKey();
31+
32+
Assert.assertTrue(gkey.getCipher().getAlgorithm()
33+
.equalsIgnoreCase(OzoneConsts.GDPR_ALGORITHM_NAME));
34+
35+
gkey.getKeyDetails().forEach(
36+
(k, v) -> Assert.assertTrue(v.length() > 0));
37+
}
38+
39+
@Test
40+
public void testKeyGenerationWithValidInput() throws Exception {
41+
GDPRSymmetricKey gkey = new GDPRSymmetricKey(
42+
"ApacheHadoopOzoneIsAnObjectStore",
43+
OzoneConsts.GDPR_ALGORITHM_NAME);
44+
45+
Assert.assertTrue(gkey.getCipher().getAlgorithm()
46+
.equalsIgnoreCase(OzoneConsts.GDPR_ALGORITHM_NAME));
47+
48+
gkey.getKeyDetails().forEach(
49+
(k, v) -> Assert.assertTrue(v.length() > 0));
50+
}
51+
52+
@Test
53+
public void testKeyGenerationWithInvalidInput() throws Exception {
54+
GDPRSymmetricKey gkey = null;
55+
try{
56+
gkey = new GDPRSymmetricKey("ozone",
57+
OzoneConsts.GDPR_ALGORITHM_NAME);
58+
} catch (IllegalArgumentException ex) {
59+
Assert.assertTrue(ex.getMessage()
60+
.equalsIgnoreCase("Secret must be exactly 32 characters"));
61+
Assert.assertTrue(gkey == null);
62+
}
63+
}
64+
65+
66+
}

0 commit comments

Comments
 (0)