aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/support/access/SupportAccess.java
blob: e5304a05d022b8a066ce328f7e9af11aedf96bcb (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.support.access;

import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Immutable state of support access, keeping history of all changes/grants. */
public class SupportAccess {

    public static final SupportAccess DISALLOWED_NO_HISTORY = new SupportAccess(List.of(), List.of());

    private final List<SupportAccessChange> changeHistory;
    private final List<SupportAccessGrant> grantHistory;

    /** public for serializer - do not use */
    public SupportAccess(List<SupportAccessChange> changeHistory, List<SupportAccessGrant> grantHistory) {
        this.changeHistory = Collections.unmodifiableList(changeHistory);
        this.grantHistory = Collections.unmodifiableList(grantHistory);
    }

    public List<SupportAccessChange> changeHistory() {
        return changeHistory;
    }

    public List<SupportAccessGrant> grantHistory() {
        return grantHistory;
    }

    public CurrentStatus currentStatus(Instant now) {
        Optional<SupportAccessChange> latestChange = changeHistory.stream().findFirst();

        if (latestChange.isEmpty() || latestChange.get().accessAllowedUntil().isEmpty() || now.isAfter(latestChange.get().accessAllowedUntil().get()))
            return new CurrentStatus(State.NOT_ALLOWED, Optional.empty(), Optional.empty());

        return new CurrentStatus(State.ALLOWED, latestChange.get().accessAllowedUntil(), Optional.of(latestChange.get().madeBy()));
    }

    public SupportAccess withAllowedUntil(Instant until, String changedBy, Instant changeTime) {
        if (!until.isAfter(changeTime))
            throw new IllegalArgumentException("Support access cannot be allowed for the past");

        verifyChangeOrdering(changeTime);
        return new SupportAccess(
                prepend(new SupportAccessChange(Optional.of(until), changeTime, changedBy), changeHistory),
                grantHistory);
    }

    public SupportAccess withDisallowed(String changedBy, Instant changeTime) {
        verifyChangeOrdering(changeTime);
        return new SupportAccess(
                prepend(new SupportAccessChange(Optional.empty(), changeTime, changedBy), changeHistory),
                grantHistory);
    }

    public SupportAccess withGrant(SupportAccessGrant supportAccessGrant) {
        return new SupportAccess(changeHistory, prepend(supportAccessGrant, grantHistory));
    }

    private void verifyChangeOrdering(Instant changeTime) {
        changeHistory.stream().findFirst().ifPresent(lastChange -> {
            if (changeTime.isBefore(lastChange.changeTime())) {
                throw new IllegalArgumentException("Support access change cannot be dated before previous change");
            }
        });
    }

    private <T> List<T> prepend(T newEntry, List<T> existingEntries) {
        return Stream.concat(Stream.of(newEntry), existingEntries.stream()) // latest change first
                .toList();
    }

    public static class CurrentStatus {
        private final State state;
        private final Optional<Instant> allowedUntil;
        private final Optional<String> allowedBy;

        private CurrentStatus(State state, Optional<Instant> allowedUntil, Optional<String> allowedBy) {
            this.state = state;
            this.allowedUntil = allowedUntil;
            this.allowedBy = allowedBy;
        }

        public State state() {
            return state;
        }

        public Optional<Instant> allowedUntil() {
            return allowedUntil;
        }

        public Optional<String> allowedBy() {
            return allowedBy;
        }
    }

    public enum State {
        NOT_ALLOWED,
        ALLOWED
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SupportAccess that = (SupportAccess) o;
        return changeHistory.equals(that.changeHistory) && grantHistory.equals(that.grantHistory);
    }

    @Override
    public int hashCode() {
        return Objects.hash(changeHistory, grantHistory);
    }

    @Override
    public String toString() {
        return "SupportAccess{" +
                "changeHistory=" + changeHistory +
                ", grantHistory=" + grantHistory +
                '}';
    }
}