Search in sources :

Example 21 with CertificateAuthority

use of com.yahoo.athenz.auth.impl.CertificateAuthority in project athenz by yahoo.

the class ZTSImplTest method testPostInstanceRefreshRequestByServiceCert.

@Test
public void testPostInstanceRefreshRequestByServiceCert() 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);
    Path path = Paths.get("src/test/resources/valid_provider_refresh.csr");
    String certCsr = new String(Files.readAllBytes(path));
    InstanceRefreshRequest req = new InstanceRefreshRequest().setCsr(certCsr).setKeyId("v0");
    path = Paths.get("src/test/resources/valid_provider_refresh.pem");
    String pem = new String(Files.readAllBytes(path));
    X509Certificate cert = Crypto.loadX509Certificate(pem);
    SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("athenz", "syncer", "v=S1,d=athenz;n=syncer;s=sig", 0, new CertificateAuthority());
    principal.setX509Certificate(cert);
    String publicKeyName = "athenz.syncer_v0";
    final String ztsPublicKey = "-----BEGIN PUBLIC KEY-----\n" + "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMp9ZHVDK2s/FyinpKpD7lSsU+d6TSRE\n" + "NVo6sdLrEpOaCJETsh+0Qc0knhALxBD1+B9gS5F2rAFgtug0R6savvMCAwEAAQ==\n" + "-----END PUBLIC KEY-----";
    ztsImpl.dataStore.getPublicKeyCache().put(publicKeyName, ztsPublicKey);
    ZTSAuthorizer authorizer = Mockito.mock(ZTSAuthorizer.class);
    Mockito.when(authorizer.access("update", "athenz:service", principal, null)).thenReturn(true);
    ztsImpl.authorizer = authorizer;
    HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
    Mockito.when(servletRequest.isSecure()).thenReturn(true);
    Mockito.when(servletRequest.getRemoteAddr()).thenReturn("10.0.0.1");
    ResourceContext context = createResourceContext(principal, servletRequest);
    Identity identity = ztsImpl.postInstanceRefreshRequest(context, "athenz", "syncer", req);
    assertNotNull(identity);
    assertNotNull(identity.getCertificate());
}
Also used : Path(java.nio.file.Path) X509Certificate(java.security.cert.X509Certificate) 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) CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) ServiceIdentity(com.yahoo.athenz.zms.ServiceIdentity) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) Test(org.testng.annotations.Test)

Example 22 with CertificateAuthority

use of com.yahoo.athenz.auth.impl.CertificateAuthority in project athenz by yahoo.

the class CertificateAuthorityTest method testAuthenticateRoleCertificate.

@Test
public void testAuthenticateRoleCertificate() throws Exception, IOException {
    CertificateAuthority authority = new CertificateAuthority();
    authority.initialize();
    try (InputStream inStream = new FileInputStream("src/test/resources/valid_email_x509.cert")) {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
        X509Certificate[] certs = new X509Certificate[1];
        certs[0] = cert;
        Principal principal = authority.authenticate(certs, null);
        assertNotNull(principal);
        assertEquals("athens", principal.getDomain());
        assertEquals("zts", principal.getName());
        assertEquals("sports:role.readers", principal.getRoles().get(0));
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) CertificateFactory(java.security.cert.CertificateFactory) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) Principal(com.yahoo.athenz.auth.Principal) Test(org.testng.annotations.Test)

Example 23 with CertificateAuthority

use of com.yahoo.athenz.auth.impl.CertificateAuthority in project athenz by yahoo.

the class CertificateAuthorityTest method testAuthenticateCertificate.

@Test
public void testAuthenticateCertificate() throws Exception, IOException {
    CertificateAuthority authority = new CertificateAuthority();
    authority.initialize();
    try (InputStream inStream = new FileInputStream("src/test/resources/valid_cn_x509.cert")) {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
        X509Certificate[] certs = new X509Certificate[1];
        certs[0] = cert;
        Principal principal = authority.authenticate(certs, null);
        assertNotNull(principal);
        assertEquals("athenz", principal.getDomain());
        assertEquals("syncer", principal.getName());
        assertNull(principal.getRoles());
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) CertificateFactory(java.security.cert.CertificateFactory) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) Principal(com.yahoo.athenz.auth.Principal) Test(org.testng.annotations.Test)

Example 24 with CertificateAuthority

use of com.yahoo.athenz.auth.impl.CertificateAuthority in project athenz by yahoo.

the class CertificateAuthorityTest method testHeaderAuthenticate.

@Test
public void testHeaderAuthenticate() {
    CertificateAuthority authority = new CertificateAuthority();
    authority.initialize();
    assertNull(authority.authenticate("v=U1;d=domain;n=service;s=sig", null, "GET", null));
}
Also used : CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) Test(org.testng.annotations.Test)

Example 25 with CertificateAuthority

use of com.yahoo.athenz.auth.impl.CertificateAuthority in project athenz by yahoo.

the class CertificateAuthorityTest method testGetHeader.

@Test
public void testGetHeader() {
    CertificateAuthority authority = new CertificateAuthority();
    authority.initialize();
    assertNull(authority.getHeader());
}
Also used : CertificateAuthority(com.yahoo.athenz.auth.impl.CertificateAuthority) Test(org.testng.annotations.Test)

Aggregations

CertificateAuthority (com.yahoo.athenz.auth.impl.CertificateAuthority)41 Test (org.testng.annotations.Test)38 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)34 X509Certificate (java.security.cert.X509Certificate)33 Path (java.nio.file.Path)30 ChangeLogStore (com.yahoo.athenz.zts.store.ChangeLogStore)26 DataStore (com.yahoo.athenz.zts.store.DataStore)26 MockZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.MockZMSFileChangeLogStore)26 ZMSFileChangeLogStore (com.yahoo.athenz.zts.store.impl.ZMSFileChangeLogStore)26 X509CertRecord (com.yahoo.athenz.zts.cert.X509CertRecord)24 SignedDomain (com.yahoo.athenz.zms.SignedDomain)20 InstanceCertManager (com.yahoo.athenz.zts.cert.InstanceCertManager)19 InstanceProvider (com.yahoo.athenz.instance.provider.InstanceProvider)14 InstanceConfirmation (com.yahoo.athenz.instance.provider.InstanceConfirmation)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Principal (com.yahoo.athenz.auth.Principal)6 X509CertRequest (com.yahoo.athenz.zts.cert.X509CertRequest)5 CertRecordStore (com.yahoo.athenz.zts.cert.CertRecordStore)4 CertRecordStoreConnection (com.yahoo.athenz.zts.cert.CertRecordStoreConnection)4 Authority (com.yahoo.athenz.auth.Authority)3