// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.athenz.instanceproviderservice.impl;

import com.yahoo.athenz.auth.util.Crypto;
import com.yahoo.athenz.zts.InstanceRefreshRequest;
import com.yahoo.athenz.zts.ZTSClient;
import com.yahoo.container.jdisc.athenz.AthenzIdentityProvider;
import com.yahoo.vespa.hosted.athenz.instanceproviderservice.config.AthenzProviderServiceConfig;

import javax.net.ssl.SSLContext;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

/**
 * @author bjorncs
 */
public class AthenzCertificateClient {

    private final AthenzProviderServiceConfig.Zones zoneConfig;
    private final AthenzIdentityProvider bootstrapIdentity;

    public AthenzCertificateClient(AthenzIdentityProvider bootstrapIdentity,
                                   AthenzProviderServiceConfig.Zones zoneConfig) {
        this.bootstrapIdentity = bootstrapIdentity;
        this.zoneConfig = zoneConfig;
    }

    public X509Certificate updateCertificate(PrivateKey privateKey) {
        SSLContext bootstrapSslContext = bootstrapIdentity.getIdentitySslContext();
        ZTSClient ztsClient = new ZTSClient(zoneConfig.ztsUrl(), bootstrapSslContext);
        InstanceRefreshRequest req =
                ZTSClient.generateInstanceRefreshRequest(
                        zoneConfig.domain(), zoneConfig.serviceName(), privateKey, zoneConfig.certDnsSuffix(), /*expiryTime*/0);
        req.setKeyId(Integer.toString(zoneConfig.secretVersion()));
        String pemEncoded = ztsClient.postInstanceRefreshRequest(zoneConfig.domain(), zoneConfig.serviceName(), req)
                .getCertificate();
        return Crypto.loadX509Certificate(pemEncoded);
    }

}

@@ -185,50 +185,8 @@
 l;%0A%0A
-import com.yahoo.athenz.auth.util.Crypto;%0A
 impo
@@ -336,16 +336,72 @@
 ovider;%0A
+import com.yahoo.vespa.athenz.tls.X509CertificateUtils;%0A
 import c
@@ -1753,19 +1753,8 @@
 urn 
-Crypto.load
 X509
@@ -1764,16 +1764,29 @@
 tificate
+Utils.fromPem
 (pemEnco

Use X509CertificateUtils