Search in sources :

Example 1 with InstanceRegisterToken

use of com.yahoo.athenz.zts.InstanceRegisterToken in project athenz by yahoo.

the class InstanceZTSProviderTest method testValidateRegisterTokenMismatchFields.

@Test
public void testValidateRegisterTokenMismatchFields() 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);
    // now let's use the validate method for specific cases
    StringBuilder errMsg = new StringBuilder();
    assertFalse(provider.validateRegisterToken(token.getAttestationData(), "weather", "api", "id001", false, errMsg));
    assertTrue(errMsg.toString().contains("invalid domain name"));
    // next service mismatch
    errMsg.setLength(0);
    assertFalse(provider.validateRegisterToken(token.getAttestationData(), "sports", "backend", "id001", false, errMsg));
    assertTrue(errMsg.toString().contains("invalid service name"));
    // invalid instance id
    errMsg.setLength(0);
    assertFalse(provider.validateRegisterToken(token.getAttestationData(), "sports", "api", "id002", false, errMsg));
    assertTrue(errMsg.toString().contains("invalid instance id"));
    provider.close();
    System.clearProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST);
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) InstanceZTSProvider(com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider) InstanceRegisterToken(com.yahoo.athenz.zts.InstanceRegisterToken) KeyStore(com.yahoo.athenz.auth.KeyStore) Test(org.testng.annotations.Test)

Example 2 with InstanceRegisterToken

use of com.yahoo.athenz.zts.InstanceRegisterToken in project athenz by yahoo.

the class InstanceZTSProviderTest method testGetInstanceRegisterToken.

@Test
public void testGetInstanceRegisterToken() throws IOException {
    InstanceZTSProvider provider = new InstanceZTSProvider();
    provider.initialize("provider", "com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider", null, null);
    Path path = Paths.get("./src/test/resources/unit_test_ec_private.key");
    final String keyPem = new String(Files.readAllBytes(path));
    PrivateKey privateKey = Crypto.loadPrivateKey(keyPem);
    provider.setPrivateKey(privateKey, "k0", SignatureAlgorithm.ES256);
    InstanceConfirmation confirmation = new InstanceConfirmation();
    confirmation.setDomain("sports");
    confirmation.setService("api");
    confirmation.setProvider("sys.auth.zts");
    Map<String, String> attrs = new HashMap<>();
    attrs.put(InstanceProvider.ZTS_INSTANCE_ID, "id001");
    confirmation.setAttributes(attrs);
    InstanceRegisterToken token = provider.getInstanceRegisterToken(confirmation);
    assertNotNull(token.getAttestationData());
    provider.close();
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) PrivateKey(java.security.PrivateKey) InstanceZTSProvider(com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider) InstanceRegisterToken(com.yahoo.athenz.zts.InstanceRegisterToken) Test(org.testng.annotations.Test)

Example 3 with InstanceRegisterToken

use of com.yahoo.athenz.zts.InstanceRegisterToken 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);
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) InstanceZTSProvider(com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider) InstanceRegisterToken(com.yahoo.athenz.zts.InstanceRegisterToken) KeyStore(com.yahoo.athenz.auth.KeyStore) Test(org.testng.annotations.Test)

Example 4 with InstanceRegisterToken

use of com.yahoo.athenz.zts.InstanceRegisterToken in project athenz by yahoo.

the class InstanceZTSProviderTest method testConfirmInstanceWithRegisterTokenMismatchProvider.

@Test
public void testConfirmInstanceWithRegisterTokenMismatchProvider() 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,weather.api,sports.backend");
    // 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("athenz.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);
    try {
        provider.confirmInstance(confirmation);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), ResourceException.FORBIDDEN);
    }
    // calling validation directly should fail as well
    StringBuilder errMsg = new StringBuilder();
    assertFalse(provider.validateRegisterToken(token.getAttestationData(), "sports", "api", "id001", false, errMsg));
    assertTrue(errMsg.toString().contains("token audience is not ZTS provider"));
    provider.close();
    System.clearProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST);
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) InstanceZTSProvider(com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider) InstanceRegisterToken(com.yahoo.athenz.zts.InstanceRegisterToken) ResourceException(com.yahoo.athenz.instance.provider.ResourceException) KeyStore(com.yahoo.athenz.auth.KeyStore) Test(org.testng.annotations.Test)

Example 5 with InstanceRegisterToken

use of com.yahoo.athenz.zts.InstanceRegisterToken in project athenz by yahoo.

the class InstanceZTSProviderTest method testValidateRegisterTokenMismatchProvider.

@Test
public void testValidateRegisterTokenMismatchProvider() 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,weather.api,sports.backend");
    // 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("athenz.zts");
    Map<String, String> attrs = new HashMap<>();
    attrs.put(InstanceProvider.ZTS_INSTANCE_ID, "id001");
    tokenConfirmation.setAttributes(attrs);
    InstanceRegisterToken token = provider.getInstanceRegisterToken(tokenConfirmation);
    // calling validation directly should fail with invalid provider
    StringBuilder errMsg = new StringBuilder();
    assertFalse(provider.validateRegisterToken(token.getAttestationData(), "sports", "api", "id001", false, errMsg));
    assertTrue(errMsg.toString().contains("invalid provider name"));
    provider.close();
    System.clearProperty(InstanceZTSProvider.ZTS_PROP_PRINCIPAL_LIST);
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) InstanceZTSProvider(com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider) InstanceRegisterToken(com.yahoo.athenz.zts.InstanceRegisterToken) KeyStore(com.yahoo.athenz.auth.KeyStore) Test(org.testng.annotations.Test)

Aggregations

InstanceConfirmation (com.yahoo.athenz.instance.provider.InstanceConfirmation)5 InstanceZTSProvider (com.yahoo.athenz.instance.provider.impl.InstanceZTSProvider)5 InstanceRegisterToken (com.yahoo.athenz.zts.InstanceRegisterToken)5 Path (java.nio.file.Path)5 PrivateKey (java.security.PrivateKey)5 Test (org.testng.annotations.Test)5 KeyStore (com.yahoo.athenz.auth.KeyStore)4 PublicKey (java.security.PublicKey)4 ResourceException (com.yahoo.athenz.instance.provider.ResourceException)1