use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.
the class InstanceZTSProviderTest method testConfirmInstanceWithRegisterToken.
@Test
public void testConfirmInstanceWithRegisterToken() throws IOException {
KeyStore keystore = Mockito.mock(KeyStore.class);
Mockito.when(keystore.getPublicKey("sports", "api", "v0")).thenReturn(servicePublicKeyStringK0);
System.setProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST, "sports.api");
// get our ec public key
Path path = Paths.get("./src/test/resources/unit_test_ec_public.key");
String keyPem = new String(Files.readAllBytes(path));
PublicKey publicKey = Crypto.loadPublicKey(keyPem);
InstanceZTSProvider provider = new InstanceZTSProvider();
provider.initialize("sys.auth.zts", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, keystore);
provider.signingKeyResolver.addPublicKey("k0", publicKey);
// get our private key now
path = Paths.get("./src/test/resources/unit_test_ec_private.key");
keyPem = new String(Files.readAllBytes(path));
PrivateKey privateKey = Crypto.loadPrivateKey(keyPem);
provider.setPrivateKey(privateKey, "k0", SignatureAlgorithm.ES256);
InstanceConfirmation tokenConfirmation = new InstanceConfirmation();
tokenConfirmation.setDomain("sports");
tokenConfirmation.setService("api");
tokenConfirmation.setProvider("sys.auth.zts");
Map<String, String> attrs = new HashMap<>();
attrs.put(InstanceProvider.ZTS_INSTANCE_ID, "id001");
tokenConfirmation.setAttributes(attrs);
InstanceRegisterToken token = provider.getInstanceRegisterToken(tokenConfirmation);
// generate instance confirmation
InstanceConfirmation confirmation = new InstanceConfirmation();
confirmation.setAttestationData(token.getAttestationData());
confirmation.setDomain("sports");
confirmation.setService("api");
confirmation.setProvider("sys.auth.zts");
Map<String, String> attributes = new HashMap<>();
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_DNS, "api.sports.zts.athenz.cloud,id001.instanceid.athenz.zts.athenz.cloud");
attributes.put(InstanceProvider.ZTS_INSTANCE_CSR_PUBLIC_KEY, servicePublicKeyStringK0);
attributes.put(InstanceProvider.ZTS_INSTANCE_ID, "id001");
confirmation.setAttributes(attributes);
assertNotNull(provider.confirmInstance(confirmation));
provider.close();
System.clearProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST);
}
use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.
the class InstanceZTSProviderTest method testValidateRegisterTokenNullIssueDate.
@Test
public void testValidateRegisterTokenNullIssueDate() throws IOException {
KeyStore keystore = Mockito.mock(KeyStore.class);
Mockito.when(keystore.getPublicKey("sports", "api", "v0")).thenReturn(servicePublicKeyStringK0);
System.setProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST, "sports.api");
// get our ec public key
Path path = Paths.get("./src/test/resources/unit_test_ec_public.key");
String keyPem = new String(Files.readAllBytes(path));
PublicKey publicKey = Crypto.loadPublicKey(keyPem);
InstanceZTSProvider provider = new InstanceZTSProvider();
provider.initialize("sys.auth.zts", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, keystore);
provider.signingKeyResolver.addPublicKey("k0", publicKey);
path = Paths.get("./src/test/resources/unit_test_ec_private.key");
keyPem = new String(Files.readAllBytes(path));
PrivateKey privateKey = Crypto.loadPrivateKey(keyPem);
// first generate token with no issue date
final String registerToken = Jwts.builder().setId("001").setSubject("sports.api").setIssuer("sys.auth.zts").setAudience("sys.auth.zts").claim(CLAIM_PROVIDER, "sys.auth.zts").claim(CLAIM_DOMAIN, "sports").claim(CLAIM_SERVICE, "api").claim(CLAIM_INSTANCE_ID, "id001").claim(CLAIM_CLIENT_ID, "user.athenz").setHeaderParam(HDR_KEY_ID, "k0").setHeaderParam(HDR_TOKEN_TYPE, HDR_TOKEN_JWT).signWith(privateKey, SignatureAlgorithm.ES256).compact();
// with register instance enabled, this is going to be reject since
// there is no issue date
StringBuilder errMsg = new StringBuilder();
assertFalse(provider.validateRegisterToken(registerToken, "sports", "api", "id001", true, errMsg));
assertTrue(errMsg.toString().contains("token is already expired, issued at: null"));
// with refresh option it's going to be skipped
errMsg.setLength(0);
assertTrue(provider.validateRegisterToken(registerToken, "sports", "api", "id001", false, errMsg));
provider.close();
System.clearProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST);
}
use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.
the class InstanceZTSProviderTest method testConfirmInstanceValidHostnameIpv6.
@Test
public void testConfirmInstanceValidHostnameIpv6() {
KeyStore keystore = Mockito.mock(KeyStore.class);
Mockito.when(keystore.getPublicKey("sports", "api", "v0")).thenReturn(servicePublicKeyStringK0);
HostnameResolver hostnameResolver = Mockito.mock(HostnameResolver.class);
Mockito.when(hostnameResolver.isValidHostname("hostabc.athenz.com")).thenReturn(true);
Mockito.when(hostnameResolver.getAllByName("hostabc.athenz.com")).thenReturn(new HashSet<>(Arrays.asList("10.1.1.1", "2001:db8:a0b:12f0:0:0:0:1")));
InstanceZTSProvider provider = new InstanceZTSProvider();
provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, keystore);
provider.setHostnameResolver(hostnameResolver);
PrincipalToken tokenToSign = new PrincipalToken.Builder("S1", "sports", "api").keyId("v0").salt("salt").issueTime(System.currentTimeMillis() / 1000).expirationWindow(3600).build();
tokenToSign.sign(servicePrivateKeyStringK0);
InstanceConfirmation confirmation = new InstanceConfirmation();
confirmation.setAttestationData(tokenToSign.getSignedToken());
confirmation.setDomain("sports");
confirmation.setService("api");
confirmation.setProvider("sys.auth.zts");
Map<String, String> attributes = new HashMap<>();
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_DNS, "api.sports.zts.athenz.cloud,inst1.instanceid.athenz.zts.athenz.cloud");
attributes.put(InstanceProvider.ZTS_INSTANCE_HOSTNAME, "hostabc.athenz.com");
attributes.put(InstanceProvider.ZTS_INSTANCE_CLIENT_IP, "2001:db8:a0b:12f0:0:0:0:1");
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_IP, "10.1.1.1,2001:db8:a0b:12f0:0:0:0:1");
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_URI, "athenz://instanceid/zts/hostabc.athenz.com,athenz://hostname/hostabc.athenz.com");
attributes.put(InstanceProvider.ZTS_INSTANCE_CSR_PUBLIC_KEY, servicePublicKeyStringK0);
confirmation.setAttributes(attributes);
assertNotNull(provider.confirmInstance(confirmation));
provider.close();
}
use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.
the class InstanceZTSProviderTest method testConfirmInstanceInvalidHostnameUri.
@Test
public void testConfirmInstanceInvalidHostnameUri() throws UnknownHostException {
KeyStore keystore = Mockito.mock(KeyStore.class);
Mockito.when(keystore.getPublicKey("sports", "api", "v0")).thenReturn(servicePublicKeyStringK0);
HostnameResolver hostnameResolver = Mockito.mock(HostnameResolver.class);
Mockito.when(hostnameResolver.isValidHostname("hostabc.athenz.com")).thenReturn(true);
Mockito.when(hostnameResolver.getAllByName("hostabc.athenz.com")).thenReturn(new HashSet<>(Collections.singletonList("10.1.1.1")));
InstanceZTSProvider provider = new InstanceZTSProvider();
provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, keystore);
provider.setHostnameResolver(hostnameResolver);
PrincipalToken tokenToSign = new PrincipalToken.Builder("S1", "sports", "api").keyId("v0").salt("salt").issueTime(System.currentTimeMillis() / 1000).expirationWindow(3600).build();
tokenToSign.sign(servicePrivateKeyStringK0);
InstanceConfirmation confirmation = new InstanceConfirmation();
confirmation.setAttestationData(tokenToSign.getSignedToken());
confirmation.setDomain("sports");
confirmation.setService("api");
confirmation.setProvider("sys.auth.zts");
Map<String, String> attributes = new HashMap<>();
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_DNS, "api.sports.zts.athenz.cloud,inst1.instanceid.athenz.zts.athenz.cloud");
attributes.put(InstanceProvider.ZTS_INSTANCE_HOSTNAME, "hostabc.athenz.com");
attributes.put(InstanceProvider.ZTS_INSTANCE_CLIENT_IP, "10.1.1.1");
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_IP, "10.1.1.1");
attributes.put(InstanceProvider.ZTS_INSTANCE_SAN_URI, "athenz://instanceid/zts/def.athenz.com,athenz://hostname/def.athenz.com");
attributes.put(InstanceProvider.ZTS_INSTANCE_CSR_PUBLIC_KEY, servicePublicKeyStringK0);
confirmation.setAttributes(attributes);
try {
provider.confirmInstance(confirmation);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 403);
assertTrue(ex.getMessage().contains("validate certificate request URI hostname"));
}
provider.close();
}
use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.
the class InstanceZTSProviderTest method testConfirmInstanceInvalidToken.
@Test
public void testConfirmInstanceInvalidToken() {
KeyStore keystore = Mockito.mock(KeyStore.class);
Mockito.when(keystore.getPublicKey("sports", "api", "v0")).thenReturn(servicePublicKeyStringK0);
InstanceZTSProvider provider = new InstanceZTSProvider();
provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, keystore);
PrincipalToken tokenToSign = new PrincipalToken.Builder("S1", "sports", "api").keyId("v0").salt("salt").issueTime(System.currentTimeMillis() / 1000).expirationWindow(3600).build();
tokenToSign.sign(servicePrivateKeyStringK0);
InstanceConfirmation confirmation = new InstanceConfirmation();
confirmation.setAttestationData(tokenToSign.getSignedToken().replace(";s=", ";s=abc"));
confirmation.setDomain("sports");
confirmation.setService("api");
confirmation.setProvider("sys.auth.zts");
try {
provider.confirmInstance(confirmation);
fail();
} catch (ResourceException ex) {
assertEquals(ex.getCode(), 403);
assertTrue(ex.getMessage().contains("validate Certificate Request Auth Token"));
}
provider.close();
}
Aggregations