| 
 | 1 | +/*  | 
 | 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one  | 
 | 3 | + * or more contributor license agreements. Licensed under the "Elastic License  | 
 | 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side  | 
 | 5 | + * Public License v 1"; you may not use this file except in compliance with, at  | 
 | 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public  | 
 | 7 | + * License v3.0 only", or the "Server Side Public License, v 1".  | 
 | 8 | + */  | 
 | 9 | + | 
 | 10 | +package org.elasticsearch.common.settings;  | 
 | 11 | + | 
 | 12 | +import org.elasticsearch.TransportVersion;  | 
 | 13 | +import org.elasticsearch.TransportVersions;  | 
 | 14 | +import org.elasticsearch.cluster.AbstractNamedDiffable;  | 
 | 15 | +import org.elasticsearch.cluster.ClusterState;  | 
 | 16 | +import org.elasticsearch.cluster.NamedDiff;  | 
 | 17 | +import org.elasticsearch.common.io.stream.StreamInput;  | 
 | 18 | +import org.elasticsearch.common.io.stream.StreamOutput;  | 
 | 19 | +import org.elasticsearch.xcontent.ToXContent;  | 
 | 20 | + | 
 | 21 | +import java.io.IOException;  | 
 | 22 | +import java.util.Collections;  | 
 | 23 | +import java.util.Iterator;  | 
 | 24 | +import java.util.Objects;  | 
 | 25 | + | 
 | 26 | +/**  | 
 | 27 | + * Secrets that are stored in cluster state  | 
 | 28 | + *  | 
 | 29 | + * <p>Cluster state secrets are initially loaded on each node, from a file on disk,  | 
 | 30 | + * in the format defined by {@link org.elasticsearch.common.settings.LocallyMountedSecrets}.  | 
 | 31 | + * Once the cluster is running, the master node watches the file for changes. This class  | 
 | 32 | + * propagates changes in the file-based secure settings from the master node out to other  | 
 | 33 | + * nodes.  | 
 | 34 | + *  | 
 | 35 | + * <p>Since the master node should always have settings on disk, we don't need to  | 
 | 36 | + * persist this class to saved cluster state, either on disk or in the cloud. Therefore,  | 
 | 37 | + * we have defined this {@link ClusterState.Custom} as a private custom object. Additionally,  | 
 | 38 | + * we don't want to ever write this class's secrets out in a client response, so  | 
 | 39 | + * {@link #toXContentChunked(ToXContent.Params)} returns an empty iterator.  | 
 | 40 | + */  | 
 | 41 | +public class ClusterSecrets extends AbstractNamedDiffable<ClusterState.Custom> implements ClusterState.Custom {  | 
 | 42 | + | 
 | 43 | +    /**  | 
 | 44 | +     * The name for this data class  | 
 | 45 | +     *  | 
 | 46 | +     * <p>This name will be used to identify this {@link org.elasticsearch.common.io.stream.NamedWriteable} in cluster  | 
 | 47 | +     * state. See {@link #getWriteableName()}.  | 
 | 48 | +     */  | 
 | 49 | +    public static final String TYPE = "cluster_state_secrets";  | 
 | 50 | + | 
 | 51 | +    private final SecureClusterStateSettings settings;  | 
 | 52 | +    private final long version;  | 
 | 53 | + | 
 | 54 | +    public ClusterSecrets(long version, SecureClusterStateSettings settings) {  | 
 | 55 | +        this.version = version;  | 
 | 56 | +        this.settings = settings;  | 
 | 57 | +    }  | 
 | 58 | + | 
 | 59 | +    public ClusterSecrets(StreamInput in) throws IOException {  | 
 | 60 | +        this.version = in.readLong();  | 
 | 61 | +        this.settings = new SecureClusterStateSettings(in);  | 
 | 62 | +    }  | 
 | 63 | + | 
 | 64 | +    public SecureSettings getSettings() {  | 
 | 65 | +        return new SecureClusterStateSettings(settings);  | 
 | 66 | +    }  | 
 | 67 | + | 
 | 68 | +    public long getVersion() {  | 
 | 69 | +        return version;  | 
 | 70 | +    }  | 
 | 71 | + | 
 | 72 | +    @Override  | 
 | 73 | +    public boolean isPrivate() {  | 
 | 74 | +        return true;  | 
 | 75 | +    }  | 
 | 76 | + | 
 | 77 | +    @Override  | 
 | 78 | +    public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {  | 
 | 79 | +        // never render this to the user  | 
 | 80 | +        return Collections.emptyIterator();  | 
 | 81 | +    }  | 
 | 82 | + | 
 | 83 | +    @Override  | 
 | 84 | +    public String getWriteableName() {  | 
 | 85 | +        return TYPE;  | 
 | 86 | +    }  | 
 | 87 | + | 
 | 88 | +    @Override  | 
 | 89 | +    public TransportVersion getMinimalSupportedVersion() {  | 
 | 90 | +        return TransportVersions.V_8_9_X;  | 
 | 91 | +    }  | 
 | 92 | + | 
 | 93 | +    @Override  | 
 | 94 | +    public void writeTo(StreamOutput out) throws IOException {  | 
 | 95 | +        out.writeLong(version);  | 
 | 96 | +        settings.writeTo(out);  | 
 | 97 | +    }  | 
 | 98 | + | 
 | 99 | +    public static NamedDiff<ClusterState.Custom> readDiffFrom(StreamInput in) throws IOException {  | 
 | 100 | +        return readDiffFrom(ClusterState.Custom.class, TYPE, in);  | 
 | 101 | +    }  | 
 | 102 | + | 
 | 103 | +    @Override  | 
 | 104 | +    public String toString() {  | 
 | 105 | +        return "ClusterStateSecrets{[all secret]}";  | 
 | 106 | +    }  | 
 | 107 | + | 
 | 108 | +    @Override  | 
 | 109 | +    public boolean equals(Object o) {  | 
 | 110 | +        if (this == o) return true;  | 
 | 111 | +        if (o == null || getClass() != o.getClass()) return false;  | 
 | 112 | +        ClusterSecrets that = (ClusterSecrets) o;  | 
 | 113 | +        return version == that.version && Objects.equals(settings, that.settings);  | 
 | 114 | +    }  | 
 | 115 | + | 
 | 116 | +    @Override  | 
 | 117 | +    public int hashCode() {  | 
 | 118 | +        return Objects.hash(settings, version);  | 
 | 119 | +    }  | 
 | 120 | +}  | 
0 commit comments