aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/zpe/DefaultZpe.java
blob: 4908f5d637bcf65fed2ec3483c7feb56efede0d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.zpe;

import com.yahoo.athenz.auth.token.AccessToken;
import com.yahoo.athenz.zpe.AuthZpeClient;
import com.yahoo.vespa.athenz.api.AthenzAccessToken;
import com.yahoo.vespa.athenz.api.AthenzResourceName;
import com.yahoo.vespa.athenz.api.AthenzRole;
import com.yahoo.vespa.athenz.api.ZToken;
import com.yahoo.vespa.athenz.zpe.AuthorizationResult.Type;

import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The default implementation of {@link Zpe}.
 * This implementation is currently based on the official Athenz ZPE library.
 *
 * @author bjorncs
 */
public class DefaultZpe implements Zpe {

    // Disable log spam from ZPE library
    private static final Logger fpksLogger =
            Logger.getLogger(com.yahoo.athenz.zpe.pkey.file.FilePublicKeyStore.class.getName());
    static {
        fpksLogger.setLevel(Level.SEVERE);
    }

    public DefaultZpe() {
        AuthZpeClient.init();
        // Disable access token cert offset validation to allow existing tokens with refreshed certs
        AccessToken.setAccessTokenCertOffset(-1);
    }

    @Override
    public AuthorizationResult checkAccessAllowed(ZToken roleToken, AthenzResourceName resourceName, String action) {
        StringBuilder returnedMatchedRole = new StringBuilder();
        AuthZpeClient.AccessCheckStatus rawResult =
                AuthZpeClient.allowAccess(roleToken.getRawToken(), resourceName.toResourceNameString(), action, returnedMatchedRole);
        return createResult(returnedMatchedRole, rawResult, resourceName);
    }

    @Override
    public AuthorizationResult checkAccessAllowed(X509Certificate roleCertificate, AthenzResourceName resourceName, String action) {
        StringBuilder returnedMatchedRole = new StringBuilder();
        AuthZpeClient.AccessCheckStatus rawResult =
                AuthZpeClient.allowAccess(roleCertificate, resourceName.toResourceNameString(), action, returnedMatchedRole);
        return createResult(returnedMatchedRole, rawResult, resourceName);
    }

    @Override
    public AuthorizationResult checkAccessAllowed(
            AthenzAccessToken accessToken, X509Certificate identityCertificate, AthenzResourceName resourceName, String action) {
        StringBuilder returnedMatchedRole = new StringBuilder();
        AuthZpeClient.AccessCheckStatus rawResult;
        if (identityCertificate == null) {
            rawResult = AuthZpeClient.allowAccess(accessToken.value(), resourceName.toResourceNameString(), action, returnedMatchedRole);
        } else {
            rawResult = AuthZpeClient.allowAccess(
                    accessToken.value(), identityCertificate, /*certHash*/null, resourceName.toResourceNameString(), action, returnedMatchedRole);
        }
        return createResult(returnedMatchedRole, rawResult, resourceName);
    }

    @Override
    public AuthorizationResult checkAccessAllowed(AthenzAccessToken accessToken, AthenzResourceName resourceName, String action) {
        return checkAccessAllowed(accessToken, null, resourceName, action);
    }

    private static AuthorizationResult createResult(
            StringBuilder matchedRole, AuthZpeClient.AccessCheckStatus rawResult, AthenzResourceName resourceName) {
        return new AuthorizationResult(Type.fromAccessCheckStatus(rawResult), toRole(matchedRole, resourceName));
    }

    private static AthenzRole toRole(StringBuilder rawRole, AthenzResourceName resourceName) {
        if (rawRole.length() == 0) {
            return null;
        } else {
            return new AthenzRole(resourceName.getDomain(), rawRole.toString());
        }
    }

}