Search in sources :

Example 1 with SshCertRetrieveDetails

use of com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails in project vcert-java by Venafi.

the class TppTokenConnectorATForSSH method requestAndRetrieveSshCertificateWithKeyPairProvided.

@Test
@DisplayName("TPP - Testing the requestSshCertificate() and retrieveSshCertificate() methods when KeyPair is provided")
public void requestAndRetrieveSshCertificateWithKeyPairProvided() throws VCertException, Exception {
    String keyId = TppTestUtils.getRandSshKeyId();
    // getting an SSH Key Pair with a key size of 3072 bits
    SshKeyPair pair = SshKeyPairGenerator.generateKeyPair(SshKeyPairGenerator.SSH2_RSA, 3072);
    // extracting the Public Key and adding the KeyId as comment, at the end of the Public Key
    // because TPP returns the Public Key on that way
    String publicKeyData = SshKeyUtils.getFormattedKey(pair.getPublicKey(), keyId);
    // building an SshCertificateRequest
    SshCertificateRequest req = new SshCertificateRequest().keyId(keyId).validityPeriod("4h").template(System.getenv("TPP_SSH_CA")).publicKeyData(publicKeyData).sourceAddresses(new String[] { "test.com" });
    // requesting the SSH Certificate
    String pickUpID = classUnderTest.requestSshCertificate(req);
    // setting the pickUp ID
    req.pickupID(pickUpID);
    // retrieving the Cert and details
    SshCertRetrieveDetails sshCertRetrieveDetails = classUnderTest.retrieveSshCertificate(req);
    assertEquals(publicKeyData, sshCertRetrieveDetails.publicKeyData());
    assertNotNull(sshCertRetrieveDetails.certificateData());
    Long validityPeriodFromCert = Long.parseLong(sshCertRetrieveDetails.certificateDetails().validTo()) - Long.parseLong(sshCertRetrieveDetails.certificateDetails().validFrom());
    // 4h
    assertEquals(14400L, validityPeriodFromCert.longValue());
}
Also used : SshKeyPair(com.sshtools.common.ssh.components.SshKeyPair) SshCertificateRequest(com.venafi.vcert.sdk.certificate.SshCertificateRequest) SshCertRetrieveDetails(com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with SshCertRetrieveDetails

use of com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails in project vcert-java by Venafi.

the class TppTokenConnectorATForSSH method requestAndRetrieveSshCertificate.

@Test
@DisplayName("TPP - Testing the requestSshCertificate() and retrieveSshCertificate() methods when the KeyPair is not provided and it will be generated by the Server")
public void requestAndRetrieveSshCertificate() throws VCertException, Exception {
    SshCertificateRequest req = new SshCertificateRequest().keyId(TppTestUtils.getRandSshKeyId()).validityPeriod("4h").template(System.getenv("TPP_SSH_CA")).sourceAddresses(new String[] { "test.com" });
    // requesting the SSH Certificate
    String pickUpID = classUnderTest.requestSshCertificate(req);
    // setting the pickUp ID
    req.pickupID(pickUpID);
    // setting a passphrase to the KeyPair service generated
    req.privateKeyPassphrase("my-passphrase");
    // retrieving the Cert and details
    SshCertRetrieveDetails sshCertRetrieveDetails = classUnderTest.retrieveSshCertificate(req);
    assertNotNull(sshCertRetrieveDetails.certificateData());
    // The following it should works correctly given that the passphrase is correct.
    SshKeyPair sshKeyPair = SshKeyUtils.getPrivateKey(sshCertRetrieveDetails.privateKeyData(), "my-passphrase");
    assertNotNull(sshKeyPair);
}
Also used : SshKeyPair(com.sshtools.common.ssh.components.SshKeyPair) SshCertificateRequest(com.venafi.vcert.sdk.certificate.SshCertificateRequest) SshCertRetrieveDetails(com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with SshCertRetrieveDetails

use of com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails in project vcert-java by Venafi.

the class SshCertificateRequestRetrieveWithKeyPairProvided method main.

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        // replace it by the key id value
        String keyId = "<KEY_ID>";
        // replace it by the CADN or the CA Name
        String template = "<TPP_SSH_CA>";
        // replace it by the TPP User
        String user = "<TPPUSER>";
        // replace it by the TPP Password
        String password = "<TPPPASSWORD>";
        // replace it by the TPP URL
        String baseUri = "<TPP_URL>";
        // 1. Get a VCertClient for TPP setting the scope to "ssh:manage"
        Authentication auth = Authentication.builder().user(user).password(password).scope("ssh:manage").build();
        Config config = Config.builder().connectorType(ConnectorType.TPP_TOKEN).baseUrl(baseUri).build();
        VCertTknClient client = new VCertTknClient(config);
        client.getAccessToken(auth);
        // To work with the SSH KeyPair, we are going to use some utilities from
        // maverick-synergy project. For more information, please visit https://github.com/sshtools/maverick-synergy
        // 2. Get an SSH Key Pair with a key size of 3072 bits
        SshKeyPair pair = SshKeyPairGenerator.generateKeyPair(SshKeyPairGenerator.SSH2_RSA, 3072);
        // 3. Extract the Public Key and adding the KeyId as comment, at the end of the Public Key
        // because TPP returns the Public Key on that way
        String publicKeyData = SshKeyUtils.getFormattedKey(pair.getPublicKey(), keyId);
        // 4. Get an instance of com.venafi.vcert.sdk.certificate.SshCertificateRequest class.
        SshCertificateRequest req = new SshCertificateRequest().keyId(keyId).validityPeriod(// if you omit it, then the validity period of the CIT will be used
        "4h").publicKeyData(publicKeyData).template(template);
        // .sourceAddresses(new String[]{"test.com"});
        // 5. Use the VCertClient method requestSshCertificate() to request the creation of a new
        // SSH Certificate on TPP. This will return the DN of the created SSH Certificate which
        // will be used to retrieve the created SSH Certificate.
        String pickUpID = client.requestSshCertificate(req);
        // 4. Set the pickUp ID to the SshCertificateRequest created. You can create a new one
        // but in order to avoid the boilerplate, it's preferable to use the already one created.
        req.pickupID(pickUpID);
        // 5. Use the VCertClient method retrieveSshCertificate() to retrieve the created
        // SSH Certificate on TPP. It will return an instance of SshCertRetrieveDetails which
        // will contain the Ssh Certificate Data, the Public Key, etc.
        SshCertRetrieveDetails sshCertRetrieveDetails = client.retrieveSshCertificate(req);
        client.revokeAccessToken();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : VCertTknClient(com.venafi.vcert.sdk.VCertTknClient) SshKeyPair(com.sshtools.common.ssh.components.SshKeyPair) SshCertificateRequest(com.venafi.vcert.sdk.certificate.SshCertificateRequest) Authentication(com.venafi.vcert.sdk.endpoint.Authentication) Config(com.venafi.vcert.sdk.Config) SshCertRetrieveDetails(com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails)

Example 4 with SshCertRetrieveDetails

use of com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails in project vcert-java by Venafi.

the class TppConnectorUtils method convertToSshCertRetrieveDetails.

public static SshCertRetrieveDetails convertToSshCertRetrieveDetails(TppSshCertRetrieveResponse tppSshCertRetrieveResponse) throws VCertException {
    SshCertRetrieveDetails sshCertRetrieveDetails = new SshCertRetrieveDetails();
    sshCertRetrieveDetails.certificateDetails(tppSshCertRetrieveResponse.certificateDetails());
    sshCertRetrieveDetails.privateKeyData(tppSshCertRetrieveResponse.privateKeyData());
    sshCertRetrieveDetails.publicKeyData(tppSshCertRetrieveResponse.publicKeyData());
    sshCertRetrieveDetails.certificateData(tppSshCertRetrieveResponse.certificateData());
    sshCertRetrieveDetails.guid(tppSshCertRetrieveResponse.guid());
    sshCertRetrieveDetails.dn(tppSshCertRetrieveResponse.dn());
    sshCertRetrieveDetails.caGuid(tppSshCertRetrieveResponse.caGuid());
    sshCertRetrieveDetails.cadn(tppSshCertRetrieveResponse.cadn());
    return sshCertRetrieveDetails;
}
Also used : SshCertRetrieveDetails(com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails)

Example 5 with SshCertRetrieveDetails

use of com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails in project vcert-java by Venafi.

the class TppConnectorATForSSH method requestAndRetrieveSshCertificate.

@Test
@DisplayName("TPP - Testing the requestSshCertificate() and retrieveSshCertificate() methods when the KeyPair is not provided and it will be generated by the Server")
public void requestAndRetrieveSshCertificate() throws VCertException, Exception {
    SshCertificateRequest req = new SshCertificateRequest().keyId(TppTestUtils.getRandSshKeyId()).validityPeriod("4h").template(System.getenv("TPP_SSH_CA")).sourceAddresses(new String[] { "test.com" });
    // requesting the SSH Certificate
    String pickUpID = classUnderTest.requestSshCertificate(req);
    // setting the pickUp ID
    req.pickupID(pickUpID);
    // setting a passphrase to the KeyPair service generated
    req.privateKeyPassphrase("my-passphrase");
    // retrieving the Cert and details
    SshCertRetrieveDetails sshCertRetrieveDetails = classUnderTest.retrieveSshCertificate(req);
    assertNotNull(sshCertRetrieveDetails.certificateData());
    // The following it should works correctly given that the passphrase is correct.
    SshKeyPair sshKeyPair = SshKeyUtils.getPrivateKey(sshCertRetrieveDetails.privateKeyData(), "my-passphrase");
    assertNotNull(sshKeyPair);
}
Also used : SshKeyPair(com.sshtools.common.ssh.components.SshKeyPair) SshCertificateRequest(com.venafi.vcert.sdk.certificate.SshCertificateRequest) SshCertRetrieveDetails(com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

SshCertRetrieveDetails (com.venafi.vcert.sdk.certificate.SshCertRetrieveDetails)7 SshCertificateRequest (com.venafi.vcert.sdk.certificate.SshCertificateRequest)6 SshKeyPair (com.sshtools.common.ssh.components.SshKeyPair)5 DisplayName (org.junit.jupiter.api.DisplayName)4 Test (org.junit.jupiter.api.Test)4 Config (com.venafi.vcert.sdk.Config)2 VCertTknClient (com.venafi.vcert.sdk.VCertTknClient)2 Authentication (com.venafi.vcert.sdk.endpoint.Authentication)2