|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.security.action.service; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionRequest; |
| 11 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 12 | +import org.elasticsearch.action.support.WriteRequest; |
| 13 | +import org.elasticsearch.common.Strings; |
| 14 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 15 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +import static org.elasticsearch.action.ValidateActions.addValidationError; |
| 21 | + |
| 22 | +public class CreateServiceAccountTokenRequest extends ActionRequest { |
| 23 | + |
| 24 | + private final String namespace; |
| 25 | + private final String serviceName; |
| 26 | + private final String tokenName; |
| 27 | + private WriteRequest.RefreshPolicy refreshPolicy = WriteRequest.RefreshPolicy.WAIT_UNTIL; |
| 28 | + |
| 29 | + public CreateServiceAccountTokenRequest(String namespace, String serviceName, String tokenName) { |
| 30 | + this.namespace = namespace; |
| 31 | + this.serviceName = serviceName; |
| 32 | + this.tokenName = tokenName; |
| 33 | + } |
| 34 | + |
| 35 | + public CreateServiceAccountTokenRequest(StreamInput in) throws IOException { |
| 36 | + super(in); |
| 37 | + this.namespace = in.readString(); |
| 38 | + this.serviceName = in.readString(); |
| 39 | + this.tokenName = in.readString(); |
| 40 | + this.refreshPolicy = WriteRequest.RefreshPolicy.readFrom(in); |
| 41 | + } |
| 42 | + |
| 43 | + public String getNamespace() { |
| 44 | + return namespace; |
| 45 | + } |
| 46 | + |
| 47 | + public String getServiceName() { |
| 48 | + return serviceName; |
| 49 | + } |
| 50 | + |
| 51 | + public String getTokenName() { |
| 52 | + return tokenName; |
| 53 | + } |
| 54 | + |
| 55 | + public WriteRequest.RefreshPolicy getRefreshPolicy() { |
| 56 | + return refreshPolicy; |
| 57 | + } |
| 58 | + |
| 59 | + public void setRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { |
| 60 | + this.refreshPolicy = Objects.requireNonNull(refreshPolicy, "refresh policy may not be null"); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object o) { |
| 65 | + if (this == o) |
| 66 | + return true; |
| 67 | + if (o == null || getClass() != o.getClass()) |
| 68 | + return false; |
| 69 | + CreateServiceAccountTokenRequest that = (CreateServiceAccountTokenRequest) o; |
| 70 | + return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName) |
| 71 | + && Objects.equals(tokenName, that.tokenName) && refreshPolicy == that.refreshPolicy; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int hashCode() { |
| 76 | + return Objects.hash(namespace, serviceName, tokenName, refreshPolicy); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void writeTo(StreamOutput out) throws IOException { |
| 81 | + super.writeTo(out); |
| 82 | + out.writeString(namespace); |
| 83 | + out.writeString(serviceName); |
| 84 | + out.writeString(tokenName); |
| 85 | + refreshPolicy.writeTo(out); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public ActionRequestValidationException validate() { |
| 90 | + ActionRequestValidationException validationException = null; |
| 91 | + if (Strings.isNullOrEmpty(namespace)) { |
| 92 | + validationException = addValidationError("service account namespace is required", validationException); |
| 93 | + } |
| 94 | + |
| 95 | + if (Strings.isNullOrEmpty(serviceName)) { |
| 96 | + validationException = addValidationError("service account service-name is required", validationException); |
| 97 | + } |
| 98 | + |
| 99 | + if (Strings.isNullOrEmpty(tokenName)) { |
| 100 | + validationException = addValidationError("service account token name is required", validationException); |
| 101 | + } else { |
| 102 | + if (tokenName.length() > 256) { |
| 103 | + validationException = addValidationError( |
| 104 | + "service account token name may not be more than 256 characters long", validationException); |
| 105 | + } |
| 106 | + if (tokenName.equals(tokenName.trim()) == false) { |
| 107 | + validationException = addValidationError( |
| 108 | + "service account token name may not begin or end with whitespace", validationException); |
| 109 | + } |
| 110 | + if (tokenName.startsWith("_")) { |
| 111 | + validationException = addValidationError( |
| 112 | + "service account token name may not begin with an underscore", validationException); |
| 113 | + } |
| 114 | + } |
| 115 | + return validationException; |
| 116 | + } |
| 117 | +} |
0 commit comments