aboutsummaryrefslogtreecommitdiffstats
path: root/hosted-api/src/main/java/ai/vespa/hosted/api/DefaultApiAuthenticator.java
blob: 8526db13b13f5137914b8e7d8875c918d04a89fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.api;

public class DefaultApiAuthenticator implements ai.vespa.hosted.api.ApiAuthenticator {

    /** Returns a controller client using mTLS if a key and certificate pair is provided, or signed requests otherwise. */
    @Override
    public ControllerHttpClient controller() {
        return Properties.apiCertificateFile()
                         .map(certificateFile -> ControllerHttpClient.withKeyAndCertificate(Properties.apiEndpoint(),
                                                                                            Properties.apiKeyFile(),
                                                                                            certificateFile))
                         .or(() -> Properties.apiKey().map(apiKey -> ControllerHttpClient.withSignatureKey(Properties.apiEndpoint(),
                                                                                                           apiKey,
                                                                                                           Properties.application())))
                         .orElseGet(() -> ControllerHttpClient.withSignatureKey(Properties.apiEndpoint(),
                                                                                Properties.apiKeyFile(),
                                                                                Properties.application()));
    }

}