All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.vespa.hosted.controller.api.integration.certificates.EndpointCertificate Maven / Gradle / Ivy

There is a newer version: 8.253.3
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.certificates;

import java.util.List;
import java.util.Optional;

/**
 * This holds information about an application's endpoint certificate.
 *
 * @author andreer
 */
public record EndpointCertificate(String keyName, String certName, int version, long lastRequested,
                                  String rootRequestId, // The id of the first request made for this certificate. Should not change.
                                  Optional leafRequestId, // The id of the last known request made for this certificate. Changes on refresh, may be outdated!
                                  List requestedDnsSans, String issuer, Optional expiry,
                                  Optional lastRefreshed, Optional generatedId) {

    public EndpointCertificate withGeneratedId(String generatedId) {
        return new EndpointCertificate(
                this.keyName,
                this.certName,
                this.version,
                this.lastRequested,
                this.rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                Optional.of(generatedId));
    }

    public EndpointCertificate withKeyName(String keyName) {
        return new EndpointCertificate(
                keyName,
                this.certName,
                this.version,
                this.lastRequested,
                this.rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                this.generatedId);
    }

    public EndpointCertificate withCertName(String certName) {
        return new EndpointCertificate(
                this.keyName,
                certName,
                this.version,
                this.lastRequested,
                this.rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                this.generatedId);
    }

    public EndpointCertificate withVersion(int version) {
        return new EndpointCertificate(
                this.keyName,
                this.certName,
                version,
                this.lastRequested,
                this.rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                this.generatedId);
    }

    public EndpointCertificate withLastRequested(long lastRequested) {
        return new EndpointCertificate(
                this.keyName,
                this.certName,
                this.version,
                lastRequested,
                this.rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                this.generatedId);
    }

    public EndpointCertificate withLastRefreshed(long lastRefreshed) {
        return new EndpointCertificate(
                this.keyName,
                this.certName,
                this.version,
                this.lastRequested,
                this.rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                Optional.of(lastRefreshed),
                this.generatedId);
    }

    public EndpointCertificate withRootRequestId(String rootRequestId) {
        return new EndpointCertificate(
                this.keyName,
                this.certName,
                this.version,
                this.lastRequested,
                rootRequestId,
                this.leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                this.generatedId);
    }

    public EndpointCertificate withLeafRequestId(Optional leafRequestId) {
        return new EndpointCertificate(
                this.keyName,
                this.certName,
                this.version,
                this.lastRequested,
                this.rootRequestId,
                leafRequestId,
                this.requestedDnsSans,
                this.issuer,
                this.expiry,
                this.lastRefreshed,
                this.generatedId);
    }

    /** Returns whether given DNS name matches any of the requested SANs in this */
    public boolean sanMatches(String dnsName) {
        return sanMatches(dnsName, requestedDnsSans);
    }

    static boolean sanMatches(String dnsName, List sanDnsNames) {
        return sanDnsNames.stream().anyMatch(sanDnsName -> sanMatches(dnsName, sanDnsName));
    }

    private static boolean sanMatches(String dnsName, String sanDnsName) {
        String[] sanNameParts = sanDnsName.split("\\.");
        String[] dnsNameParts = dnsName.split("\\.");
        if (sanNameParts.length != dnsNameParts.length || sanNameParts.length == 0) {
            return false;
        }
        for (int i = 0; i < sanNameParts.length; i++) {
            if (!sanNameParts[i].equals("*") && !sanNameParts[i].equals(dnsNameParts[i])) {
                return false;
            }
        }
        return true;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy