Search in sources :

Example 16 with KeyStore

use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.

the class PrincipalAuthorityTest method testValidateAuthorizedServiceNoSignature.

@Test
public void testValidateAuthorizedServiceNoSignature() throws IOException {
    PrincipalAuthority serviceAuthority = new PrincipalAuthority();
    KeyStore keyStore = new KeyStoreMock();
    serviceAuthority.setKeyStore(keyStore);
    long issueTime = System.currentTimeMillis() / 1000;
    // Create and sign token
    List<String> authorizedServices = new ArrayList<>();
    authorizedServices.add("coretech.storage");
    authorizedServices.add("media.storage");
    PrincipalToken userTokenToSign = new PrincipalToken.Builder(usrVersion, usrDomain, usrName).salt(salt).issueTime(issueTime).authorizedServices(authorizedServices).expirationWindow(expirationTime).build();
    userTokenToSign.sign(servicePrivateKeyStringK0);
    // Create a token for validation using the signed data
    StringBuilder errMsg = new StringBuilder();
    assertNull(serviceAuthority.validateAuthorizeService(userTokenToSign, errMsg));
}
Also used : ArrayList(java.util.ArrayList) PrincipalToken(com.yahoo.athenz.auth.token.PrincipalToken) KeyStore(com.yahoo.athenz.auth.KeyStore) PrincipalAuthority(com.yahoo.athenz.auth.impl.PrincipalAuthority) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with KeyStore

use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.

the class RoleAuthorityTest method testIsWriteOperationNull.

@Test
public void testIsWriteOperationNull() throws IOException {
    RoleAuthority roleAuthority = new RoleAuthority();
    roleAuthority.initialize();
    KeyStore keyStore = new KeyStoreMock();
    roleAuthority.setKeyStore(keyStore);
    // Add some roles
    List<String> roles = new ArrayList<String>();
    roles.add("storage.tenant.weather.updater");
    // Create and sign token with keyVersion = 0
    RoleToken roleToken = new RoleToken.Builder(rolVersion, svcDomain, roles).salt(salt).ip("127.0.0.1").expirationWindow(expirationTime).principal(".").keyId(testKeyVersionK0).build();
    roleToken.sign(ztsPrivateKeyStringK0);
    Principal principal = roleAuthority.authenticate(roleToken.getSignedToken(), "127.0.0.2", null, null);
    assertNotNull(principal);
}
Also used : RoleAuthority(com.yahoo.athenz.auth.impl.RoleAuthority) ArrayList(java.util.ArrayList) KeyStore(com.yahoo.athenz.auth.KeyStore) Principal(com.yahoo.athenz.auth.Principal) RoleToken(com.yahoo.athenz.auth.token.RoleToken) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 18 with KeyStore

use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.

the class RoleAuthorityTest method testAuthenticateIlligal.

@Test
public void testAuthenticateIlligal() throws IOException {
    RoleAuthority roleAuthority = new RoleAuthority();
    roleAuthority.initialize();
    Principal principal = roleAuthority.authenticate("", "10.72.118.45", "GET", null);
    assertNull(principal);
    KeyStore keyStore = new KeyStoreMock();
    roleAuthority.setKeyStore(keyStore);
    // Add some roles
    List<String> roles = new ArrayList<String>();
    roles.add("storage.tenant.weather.updater");
    // Create and sign token with keyVersion = 0
    RoleToken roleToken = new RoleToken.Builder(rolVersion, svcDomain, roles).salt(salt).ip("127.0.0.1").expirationWindow(expirationTime).principal(".").keyId(testKeyVersionK0).build();
    roleToken.sign(ztsPrivateKeyStringK0);
    principal = roleAuthority.authenticate(roleToken.getSignedToken(), "127.0.0.2", "DELETE", null);
    assertNull(principal);
    roleToken = new RoleToken.Builder(rolVersion, svcDomain, roles).salt(salt).ip("127.0.0.1").expirationWindow(expirationTime).principal("illigal.joe").keyId(testKeyVersionK0).build();
    roleToken.sign(ztsPrivateKeyStringK0);
    principal = roleAuthority.authenticate(roleToken.getSignedToken(), "127.0.0.2", "DELETE", null);
    assertNotNull(principal);
}
Also used : RoleAuthority(com.yahoo.athenz.auth.impl.RoleAuthority) ArrayList(java.util.ArrayList) KeyStore(com.yahoo.athenz.auth.KeyStore) Principal(com.yahoo.athenz.auth.Principal) RoleToken(com.yahoo.athenz.auth.token.RoleToken) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 19 with KeyStore

use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.

the class PrincipalAuthorityTest method testValidateAuthorizedServiceMultiple.

@Test
public void testValidateAuthorizedServiceMultiple() throws IOException {
    PrincipalAuthority serviceAuthority = new PrincipalAuthority();
    KeyStore keyStore = new KeyStoreMock();
    serviceAuthority.setKeyStore(keyStore);
    long issueTime = System.currentTimeMillis() / 1000;
    // Create and sign token
    List<String> authorizedServices = new ArrayList<>();
    authorizedServices.add("sports.fantasy");
    authorizedServices.add("sports.hockey");
    PrincipalToken userTokenToSign = new PrincipalToken.Builder(usrVersion, usrDomain, usrName).salt(salt).issueTime(issueTime).expirationWindow(expirationTime).authorizedServices(authorizedServices).build();
    userTokenToSign.sign(servicePrivateKeyStringK0);
    // now let's sign the token for an authorized service
    userTokenToSign.signForAuthorizedService("sports.fantasy", "1", servicePrivateKeyStringK1);
    // Create a token for validation using the signed data
    StringBuilder errMsg = new StringBuilder();
    assertEquals(serviceAuthority.validateAuthorizeService(userTokenToSign, errMsg), "sports.fantasy");
}
Also used : ArrayList(java.util.ArrayList) PrincipalToken(com.yahoo.athenz.auth.token.PrincipalToken) KeyStore(com.yahoo.athenz.auth.KeyStore) PrincipalAuthority(com.yahoo.athenz.auth.impl.PrincipalAuthority) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 20 with KeyStore

use of com.yahoo.athenz.auth.KeyStore in project athenz by yahoo.

the class PrincipalAuthorityTest method testGetPublicKeyKeyServiceInvalid.

@Test
public void testGetPublicKeyKeyServiceInvalid() {
    PrincipalAuthority serviceAuthority = new PrincipalAuthority();
    KeyStore keyStore = Mockito.mock(KeyStore.class);
    serviceAuthority.setKeyStore(keyStore);
    Mockito.when(keyStore.getPublicKey("sys.auth", "zms", "v1")).thenReturn("zms-key");
    Mockito.when(keyStore.getPublicKey("sys.auth", "zts", "v1")).thenReturn("zts-key");
    Mockito.when(keyStore.getPublicKey("athenz", "svc", "v1")).thenReturn("athenz-key");
    String key = serviceAuthority.getPublicKey("athenz", "svc", "bondo", "v1", false);
    assertEquals(key, "athenz-key");
}
Also used : KeyStore(com.yahoo.athenz.auth.KeyStore) PrincipalAuthority(com.yahoo.athenz.auth.impl.PrincipalAuthority) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

KeyStore (com.yahoo.athenz.auth.KeyStore)23 BeforeTest (org.testng.annotations.BeforeTest)23 Test (org.testng.annotations.Test)23 PrincipalAuthority (com.yahoo.athenz.auth.impl.PrincipalAuthority)16 ArrayList (java.util.ArrayList)13 Principal (com.yahoo.athenz.auth.Principal)11 PrincipalToken (com.yahoo.athenz.auth.token.PrincipalToken)9 RoleAuthority (com.yahoo.athenz.auth.impl.RoleAuthority)7 RoleToken (com.yahoo.athenz.auth.token.RoleToken)7