aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeagent/UserNamespace.java
blob: 40a8ab169239f845ddf023dd39760db99889933c (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.nodeagent;

import java.util.Objects;

/**
 * @author freva
 */
public class UserNamespace {

    /**
     * IDs outside the ID range are translated to the overflow ID before being written to disk:
     * https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/Documentation/admin-guide/sysctl/fs.rst#overflowgid--overflowuid
     * Real value in /proc/sys/fs/overflowuid or overflowgid, hardcode default value*/
    private static final int OVERFLOW_ID = 65_534;

    private final int uidOffset;
    private final int gidOffset;
    private final int idRangeSize;

    public UserNamespace(int uidOffset, int gidOffset, int idRangeSize) {
        this.uidOffset = uidOffset;
        this.gidOffset = gidOffset;
        this.idRangeSize = idRangeSize;
    }

    public int userIdOnHost(int containerUid) { return toHostId(containerUid, uidOffset, idRangeSize); }
    public int groupIdOnHost(int containerGid) { return toHostId(containerGid, gidOffset, idRangeSize); }
    public int userIdInContainer(int hostUid) { return toContainerId(hostUid, uidOffset, idRangeSize); }
    public int groupIdInContainer(int hostGid) { return toContainerId(hostGid, gidOffset, idRangeSize); }

    public int idRangeSize() { return idRangeSize; }
    public int overflowId() { return OVERFLOW_ID; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserNamespace that = (UserNamespace) o;
        return uidOffset == that.uidOffset && gidOffset == that.gidOffset && idRangeSize == that.idRangeSize;
    }

    @Override
    public int hashCode() {
        return Objects.hash(uidOffset, gidOffset, idRangeSize);
    }

    @Override
    public String toString() {
        return "UserNamespace{" +
                "uidOffset=" + uidOffset +
                ", gidOffset=" + gidOffset +
                ", idRangeSize=" + idRangeSize +
                '}';
    }

    private static int toHostId(int containerId, int idOffset, int idRangeSize) {
        if (containerId < 0 || containerId > idRangeSize)
            throw new IllegalArgumentException("Invalid container id: " + containerId);
        return idOffset + containerId;
    }

    private static int toContainerId(int hostId, int idOffset, int idRangeSize) {
        hostId = hostId - idOffset;
        return hostId < 0 || hostId >= idRangeSize ? OVERFLOW_ID : hostId;
    }
}