Search in sources :

Example 21 with ChangeLogStore

use of com.yahoo.athenz.zts.store.ChangeLogStore in project athenz by yahoo.

the class ZTSImplTest method testGetRoleTokenNoRoleMatchAuditLog.

@Test
public void testGetRoleTokenNoRoleMatchAuditLog() {
    HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(servletRequest.getRemoteAddr()).thenReturn("99.88.77.66");
    Mockito.when(servletRequest.isSecure()).thenReturn(true);
    final java.util.Set<String> aLogMsgs = new java.util.HashSet<String>();
    AuditLogger alogger = new AuditLogger() {

        public void log(String logMsg, String msgVersionTag) {
            aLogMsgs.add(logMsg);
        }

        public void log(AuditLogMsgBuilder msgBldr) {
            String msg = msgBldr.build();
            aLogMsgs.add(msg);
        }

        @Override
        public AuditLogMsgBuilder getMsgBuilder() {
            return new DefaultAuditLogMsgBuilder();
        }
    };
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    ztsImpl.auditLogger = alogger;
    ZTSImpl.serverHostName = "localhost";
    SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", true);
    store.processDomain(signedDomain, false);
    Principal principal = SimplePrincipal.create("user_domain", "invalidUser", "v=U1;d=user_domain;n=invalidUser;s=signature", 0, null);
    ResourceContext context = createResourceContext(principal, servletRequest);
    try {
        ztsImpl.getRoleToken(context, "coretech", null, Integer.valueOf(600), Integer.valueOf(1200), null);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 403);
    }
    for (String msg : aLogMsgs) {
        assertTrue(msg.contains("ERROR=(Principal Has No Access to Domain)"));
        assertTrue(msg.contains("CLIENT-IP=(99.88.77.66)"));
        assertTrue(msg.contains("WHO=(who-name=invalidUser,who-domain=user_domain,who-fullname=user_domain.invalidUser)"));
        break;
    }
}
Also used : DefaultAuditLogMsgBuilder(com.yahoo.athenz.common.server.log.impl.DefaultAuditLogMsgBuilder) DefaultAuditLogger(com.yahoo.athenz.common.server.log.impl.DefaultAuditLogger) AuditLogger(com.yahoo.athenz.common.server.log.AuditLogger) DefaultAuditLogMsgBuilder(com.yahoo.athenz.common.server.log.impl.DefaultAuditLogMsgBuilder) AuditLogMsgBuilder(com.yahoo.athenz.common.server.log.AuditLogMsgBuilder) HttpServletRequest(javax.servlet.http.HttpServletRequest) ChangeLogStore(com.yahoo.athenz.zts.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) DataStore(com.yahoo.athenz.zts.store.DataStore) SignedDomain(com.yahoo.athenz.zms.SignedDomain) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) Principal(com.yahoo.athenz.auth.Principal) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 22 with ChangeLogStore

use of com.yahoo.athenz.zts.store.ChangeLogStore in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRefreshInformationInvalidCSR.

@Test
public void testPostInstanceRefreshInformationInvalidCSR() throws IOException {
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    SignedDomain providerDomain = signedAuthorizedProviderDomain();
    store.processDomain(providerDomain, false);
    SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
    store.processDomain(tenantDomain, false);
    InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
    X509CertRecord certRecord = new X509CertRecord();
    certRecord.setInstanceId("1001");
    certRecord.setProvider("athenz.provider");
    certRecord.setService("athenz.production");
    certRecord.setCurrentSerial("16503746516960996918");
    certRecord.setPrevSerial("16503746516960996918");
    Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001")).thenReturn(certRecord);
    ztsImpl.instanceCertManager = instanceManager;
    InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr("csr");
    CertificateAuthority certAuthority = new CertificateAuthority();
    SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
    ResourceContext context = createResourceContext(principal);
    try {
        ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 400);
        assertTrue(ex.getMessage().contains("unable to parse PKCS10 CSR"));
    }
}
Also used : InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) ChangeLogStore(com.yahoo.athenz.zts.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) DataStore(com.yahoo.athenz.zts.store.DataStore) SignedDomain(com.yahoo.athenz.zms.SignedDomain) CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) X509CertRecord(com.yahoo.athenz.zts.cert.X509CertRecord) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) Test(org.testng.annotations.Test)

Example 23 with ChangeLogStore

use of com.yahoo.athenz.zts.store.ChangeLogStore in project athenz by yahoo.

the class ZTSImplTest method setup.

@BeforeMethod
public void setup() {
    // we want to make sure we start we clean dir structure
    ZMSFileChangeLogStore.deleteDirectory(new File(ZTS_DATA_STORE_PATH));
    String privKeyName = System.getProperty(FilePrivateKeyStore.ATHENZ_PROP_PRIVATE_KEY);
    File privKeyFile = new File(privKeyName);
    String privKey = Crypto.encodedFile(privKeyFile);
    privateKey = Crypto.loadPrivateKey(Crypto.ybase64DecodeString(privKey));
    /* create our data store */
    roleTokenDefaultTimeout = 2400;
    System.setProperty(ZTSConsts.ZTS_PROP_ROLE_TOKEN_DEFAULT_TIMEOUT, Integer.toString(roleTokenDefaultTimeout));
    roleTokenMaxTimeout = 96000;
    System.setProperty(ZTSConsts.ZTS_PROP_ROLE_TOKEN_MAX_TIMEOUT, Integer.toString(roleTokenMaxTimeout));
    System.setProperty(ZTSConsts.ZTS_PROP_AUTHORIZED_PROXY_USERS, "user_domain.proxy-user1,user_domain.proxy-user2");
    ChangeLogStore structStore = new MockZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    CloudStore cloudStore = new CloudStore(null);
    cloudStore.setHttpClient(null);
    System.setProperty(ZTSConsts.ZTS_PROP_SELF_SIGNER_PRIVATE_KEY_FNAME, "src/test/resources/private_encrypted.key");
    System.setProperty(ZTSConsts.ZTS_PROP_SELF_SIGNER_PRIVATE_KEY_PASSWORD, "athenz");
    ZMSFileChangeLogStore.deleteDirectory(new File("/tmp/zts_server_cert_store"));
    System.setProperty(ZTSConsts.ZTS_PROP_CERT_FILE_STORE_PATH, "/tmp/zts_server_cert_store");
    store = new DataStore(structStore, cloudStore);
    zts = new ZTSImpl(cloudStore, store);
    ZTSImpl.serverHostName = "localhost";
    authorizer = new ZTSAuthorizer(store);
}
Also used : ChangeLogStore(com.yahoo.athenz.zts.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) MockCloudStore(com.yahoo.athenz.zts.store.MockCloudStore) CloudStore(com.yahoo.athenz.zts.store.CloudStore) DataStore(com.yahoo.athenz.zts.store.DataStore) File(java.io.File) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 24 with ChangeLogStore

use of com.yahoo.athenz.zts.store.ChangeLogStore in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRefreshInformationIdentityFailure.

@Test
public void testPostInstanceRefreshInformationIdentityFailure() throws IOException {
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    SignedDomain providerDomain = signedAuthorizedProviderDomain();
    store.processDomain(providerDomain, false);
    SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
    store.processDomain(tenantDomain, false);
    Path path = Paths.get("src/test/resources/athenz.instanceid.csr");
    String certCsr = new String(Files.readAllBytes(path));
    InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
    InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
    InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider");
    InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
    Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient);
    Mockito.when(providerClient.refreshInstance(Mockito.any())).thenReturn(confirmation);
    X509CertRecord certRecord = new X509CertRecord();
    certRecord.setInstanceId("1001");
    certRecord.setProvider("athenz.provider");
    certRecord.setService("athenz.production");
    certRecord.setCurrentSerial("16503746516960996918");
    certRecord.setPrevSerial("16503746516960996918");
    Mockito.when(instanceManager.getX509CertRecord("athenz.provider", "1001")).thenReturn(certRecord);
    Mockito.when(instanceManager.updateX509CertRecord(Mockito.any())).thenReturn(true);
    path = Paths.get("src/test/resources/athenz.instanceid.pem");
    String pem = new String(Files.readAllBytes(path));
    Mockito.doReturn(null).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt());
    ztsImpl.instanceProviderManager = instanceProviderManager;
    ztsImpl.instanceCertManager = instanceManager;
    InstanceRefreshInformation info = new InstanceRefreshInformation().setCsr(certCsr);
    CertificateAuthority certAuthority = new CertificateAuthority();
    SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "production", "v=S1;d=athenz;n=production;s=signature", 0, certAuthority);
    X509Certificate cert = Crypto.loadX509Certificate(pem);
    principal.setX509Certificate(cert);
    ResourceContext context = createResourceContext(principal);
    try {
        ztsImpl.postInstanceRefreshInformation(context, "athenz.provider", "athenz", "production", "1001", info);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 500);
        assertTrue(ex.getMessage().contains("unable to generate identity"));
    }
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) X509CertRecord(com.yahoo.athenz.zts.cert.X509CertRecord) X509Certificate(java.security.cert.X509Certificate) ChangeLogStore(com.yahoo.athenz.zts.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) DataStore(com.yahoo.athenz.zts.store.DataStore) SignedDomain(com.yahoo.athenz.zms.SignedDomain) CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) InstanceProvider(com.yahoo.athenz.instance.provider.InstanceProvider) Test(org.testng.annotations.Test)

Example 25 with ChangeLogStore

use of com.yahoo.athenz.zts.store.ChangeLogStore in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRegisterInformationSSHIdentityFailure.

@Test
public void testPostInstanceRegisterInformationSSHIdentityFailure() throws IOException {
    ChangeLogStore structStore = new ZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", privateKey, "0");
    DataStore store = new DataStore(structStore, null);
    ZTSImpl ztsImpl = new ZTSImpl(mockCloudStore, store);
    SignedDomain providerDomain = signedAuthorizedProviderDomain();
    store.processDomain(providerDomain, false);
    SignedDomain tenantDomain = signedBootstrapTenantDomain("athenz.provider", "athenz", "production");
    store.processDomain(tenantDomain, false);
    Path path = Paths.get("src/test/resources/athenz.instanceid.csr");
    String certCsr = new String(Files.readAllBytes(path));
    InstanceProviderManager instanceProviderManager = Mockito.mock(InstanceProviderManager.class);
    InstanceProvider providerClient = Mockito.mock(InstanceProvider.class);
    InstanceConfirmation confirmation = new InstanceConfirmation().setDomain("athenz").setService("production").setProvider("athenz.provider");
    InstanceCertManager instanceManager = Mockito.spy(ztsImpl.instanceCertManager);
    Mockito.when(instanceProviderManager.getProvider("athenz.provider")).thenReturn(providerClient);
    Mockito.when(providerClient.confirmInstance(Mockito.any())).thenReturn(confirmation);
    Mockito.when(instanceManager.insertX509CertRecord(Mockito.any())).thenReturn(true);
    path = Paths.get("src/test/resources/athenz.instanceid.pem");
    String pem = new String(Files.readAllBytes(path));
    InstanceIdentity identity = new InstanceIdentity().setName("athenz.production").setX509Certificate(pem);
    Mockito.doReturn(identity).when(instanceManager).generateIdentity(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyInt());
    ztsImpl.instanceProviderManager = instanceProviderManager;
    ztsImpl.instanceCertManager = instanceManager;
    InstanceRegisterInformation info = new InstanceRegisterInformation().setAttestationData("attestationData").setCsr(certCsr).setDomain("athenz").setService("production").setProvider("athenz.provider").setToken(false);
    Mockito.doReturn(false).when(instanceManager).generateSshIdentity(Mockito.any(), Mockito.any(), Mockito.any());
    ResourceContext context = createResourceContext(null);
    PostInstanceRegisterInformationResult result = new PostInstanceRegisterInformationResult(context);
    int code = 0;
    try {
        ztsImpl.postInstanceRegisterInformation(context, info, result);
    } catch (WebApplicationException ex) {
        code = ex.getResponse().getStatus();
    }
    assertEquals(code, 201);
    assertNull(info.getSsh());
}
Also used : Path(java.nio.file.Path) InstanceConfirmation(com.yahoo.athenz.instance.provider.InstanceConfirmation) InstanceCertManager(com.yahoo.athenz.zts.cert.InstanceCertManager) WebApplicationException(javax.ws.rs.WebApplicationException) ChangeLogStore(com.yahoo.athenz.zts.store.ChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) MockZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore) ZMSFileChangeLogStore(com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore) DataStore(com.yahoo.athenz.zts.store.DataStore) SignedDomain(com.yahoo.athenz.zms.SignedDomain) InstanceProvider(com.yahoo.athenz.instance.provider.InstanceProvider) Test(org.testng.annotations.Test)

Aggregations

ChangeLogStore (com.yahoo.athenz.zts.store.ChangeLogStore)66 MockZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore)64 ZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore)64 Test (org.testng.annotations.Test)64 DataStore (com.yahoo.athenz.zts.store.DataStore)63 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)45 Path (java.nio.file.Path)44 SignedDomain (com.yahoo.athenz.zms.SignedDomain)38 CertificateAuthority (com.yahoo.athenz.auth.impl.CertificateAuthority)26 InstanceCertManager (com.yahoo.athenz.zts.cert.InstanceCertManager)26 X509Certificate (java.security.cert.X509Certificate)24 InstanceProvider (com.yahoo.athenz.instance.provider.InstanceProvider)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 InstanceConfirmation (com.yahoo.athenz.instance.provider.InstanceConfirmation)18 X509CertRecord (com.yahoo.athenz.zts.cert.X509CertRecord)18 WebApplicationException (javax.ws.rs.WebApplicationException)12 PrincipalAuthority (com.yahoo.athenz.auth.impl.PrincipalAuthority)11 Principal (com.yahoo.athenz.auth.Principal)8 IOException (java.io.IOException)8 AuditLogMsgBuilder (com.yahoo.athenz.common.server.log.AuditLogMsgBuilder)6