-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-auth-server): introduced key_ops for granular map of crypto…
… service to rotation profile #3415 (#3642) * feat(jans-auth-server): introduced key_ops for granular map of crypto service to rotation profile #3415 * fix(jans-auth-server): fixed key rotation and added test to key generator context #3415 * minor code improvements #3415
- Loading branch information
Showing
12 changed files
with
429 additions
and
157 deletions.
There are no files selected for viewing
201 changes: 104 additions & 97 deletions
201
jans-auth-server/client/src/main/java/io/jans/as/client/util/KeyGenerator.java
Large diffs are not rendered by default.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
jans-auth-server/client/src/main/java/io/jans/as/client/util/KeyGeneratorContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.jans.as.client.util; | ||
|
||
import io.jans.as.model.crypto.AbstractCryptoProvider; | ||
import io.jans.as.model.jwk.KeyOps; | ||
|
||
import java.util.Calendar; | ||
import java.util.GregorianCalendar; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public class KeyGeneratorContext { | ||
|
||
private TestPropFile testPropFile; | ||
private AbstractCryptoProvider cryptoProvider; | ||
private int keyLength; | ||
|
||
private int expirationDays; | ||
private int expirationHours; | ||
private Calendar expiration; | ||
private KeyOps keyOps; | ||
|
||
public void calculateExpiration() { | ||
Calendar calendar = new GregorianCalendar(); | ||
calendar.add(Calendar.DATE, getExpirationDays()); | ||
calendar.add(Calendar.HOUR, getExpirationHours()); | ||
this.expiration = calendar; | ||
} | ||
|
||
public long getExpirationForKeyOps(KeyOps keyOps) { | ||
if (expiration == null) { | ||
calculateExpiration(); | ||
} | ||
if (keyOps == KeyOps.SSA) { | ||
Calendar calendar = new GregorianCalendar(); | ||
calendar.add(Calendar.YEAR, 50); | ||
return calendar.getTimeInMillis(); | ||
} | ||
return expiration.getTimeInMillis(); | ||
} | ||
|
||
public KeyOps getKeyOps() { | ||
return keyOps; | ||
} | ||
|
||
public void setKeyOps(KeyOps keyOps) { | ||
this.keyOps = keyOps; | ||
} | ||
|
||
public Calendar getExpiration() { | ||
return expiration; | ||
} | ||
|
||
public void setExpiration(Calendar expiration) { | ||
this.expiration = expiration; | ||
} | ||
|
||
public int getKeyLength() { | ||
return keyLength; | ||
} | ||
|
||
public void setKeyLength(int keyLength) { | ||
this.keyLength = keyLength; | ||
} | ||
|
||
public TestPropFile getTestPropFile() { | ||
return testPropFile; | ||
} | ||
|
||
public void setTestPropFile(TestPropFile testPropFile) { | ||
this.testPropFile = testPropFile; | ||
} | ||
|
||
public AbstractCryptoProvider getCryptoProvider() { | ||
return cryptoProvider; | ||
} | ||
|
||
public void setCryptoProvider(AbstractCryptoProvider cryptoProvider) { | ||
this.cryptoProvider = cryptoProvider; | ||
} | ||
|
||
public int getExpirationDays() { | ||
return expirationDays; | ||
} | ||
|
||
public void setExpirationDays(int expirationDays) { | ||
this.expirationDays = expirationDays; | ||
} | ||
|
||
public int getExpirationHours() { | ||
return expirationHours; | ||
} | ||
|
||
public void setExpirationHours(int expirationHours) { | ||
this.expirationHours = expirationHours; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
jans-auth-server/client/src/main/java/io/jans/as/client/util/TestPropFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.jans.as.client.util; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public class TestPropFile { | ||
|
||
private static final String TEST_PROP_FILE = "test_prop_file"; | ||
|
||
private String propFile; | ||
|
||
private List<String> records = new ArrayList<>(); | ||
|
||
public TestPropFile(String propFile) { | ||
this.propFile = propFile; | ||
} | ||
|
||
public static TestPropFile create(CommandLine cmd) { | ||
if (cmd.hasOption(TEST_PROP_FILE)) { | ||
return new TestPropFile(cmd.getOptionValue(TEST_PROP_FILE)); | ||
} | ||
return new TestPropFile(null); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return StringUtils.isBlank(propFile); | ||
} | ||
|
||
public boolean shouldGenerate() { | ||
return StringUtils.isNotBlank(propFile); | ||
} | ||
|
||
public void add(String record) { | ||
if (shouldGenerate()) | ||
records.add(record); | ||
} | ||
|
||
public void generate() throws IOException { | ||
if (isEmpty()) { | ||
return; | ||
} | ||
|
||
try (FileOutputStream fosTestPropFile = new FileOutputStream(propFile)) { | ||
for (String rec : records) { | ||
fosTestPropFile.write(rec.getBytes()); | ||
fosTestPropFile.write("\n".getBytes()); | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
jans-auth-server/client/src/test/java/io/jans/as/client/util/KeyGeneratorContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.jans.as.client.util; | ||
|
||
import io.jans.as.model.jwk.KeyOps; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Calendar; | ||
|
||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public class KeyGeneratorContextTest { | ||
|
||
@Test | ||
public void getExpirationForKeyOps_forConnectKeyOps_shouldReturnPassedExpiration() { | ||
KeyGeneratorContext context = new KeyGeneratorContext(); | ||
context.setExpirationHours(1); | ||
|
||
final long expirationForKeyOps = context.getExpirationForKeyOps(KeyOps.CONNECT); | ||
|
||
assertTrue(expirationForKeyOps < futureIn2Hours()); | ||
} | ||
|
||
@Test | ||
public void getExpirationForKeyOps_forSSAKeyOps_shouldReturnExpirationFarInFuture() { | ||
KeyGeneratorContext context = new KeyGeneratorContext(); | ||
context.setExpirationHours(1); | ||
|
||
final long expirationForKeyOps = context.getExpirationForKeyOps(KeyOps.SSA); | ||
|
||
assertTrue(expirationForKeyOps > futureIn2Hours()); | ||
} | ||
|
||
private long futureIn2Hours() { | ||
Calendar future2hours = Calendar.getInstance(); | ||
future2hours.add(2, Calendar.HOUR_OF_DAY); | ||
return future2hours.getTimeInMillis(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.