aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/tls/verification_result.h
blob: 7c694813e0fcb00746b63d74270c0c64bbfdbd12 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "capability_set.h"
#include <vespa/vespalib/stllike/string.h>
#include <iosfwd>

namespace vespalib { class asciistream; }

namespace vespalib::net::tls {

/**
 * The result of evaluating configured mTLS authorization rules against the
 * credentials presented by a successfully authenticated peer certificate.
 *
 * This result contains the union set of all capabilities granted by the matching
 * authorization rules. If no rules matched, the set will be empty. The capability
 * set will also be empty for a default-constructed instance.
 *
 * It is possible for a VerificationResult to be successful but with an empty
 * capability set. If capabilities are enforced, this will effectively only
 * allow mTLS handshakes to go through, allowing rudimentary health checking.
 */
class VerificationResult {
    CapabilitySet _granted_capabilities;
    bool          _authorized;

    VerificationResult(bool authorized, CapabilitySet granted_capabilities) noexcept;
public:
    VerificationResult() noexcept; // Unauthorized by default
    VerificationResult(const VerificationResult&) noexcept;
    VerificationResult& operator=(const VerificationResult&) noexcept;
    VerificationResult(VerificationResult&&) noexcept;
    VerificationResult& operator=(VerificationResult&&) noexcept;
    ~VerificationResult();

    // Returns true iff the peer matched at least one policy or authorization is not enforced.
    [[nodiscard]] bool success() const noexcept {
        return _authorized;
    }

    [[nodiscard]] const CapabilitySet& granted_capabilities() const noexcept {
        return _granted_capabilities;
    }

    void print(asciistream& os) const;

    static VerificationResult make_authorized_with_capabilities(CapabilitySet granted_capabilities) noexcept;
    static VerificationResult make_authorized_with_all_capabilities() noexcept;
    static VerificationResult make_not_authorized() noexcept;
};

asciistream& operator<<(asciistream&, const VerificationResult&);
std::ostream& operator<<(std::ostream&, const VerificationResult&);
string to_string(const VerificationResult&);

}