|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.authandtls.credentialhelper; |
| 16 | + |
| 17 | +import com.google.auto.value.AutoValue; |
| 18 | +import com.google.common.base.Preconditions; |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import com.google.errorprone.annotations.Immutable; |
| 22 | +import com.google.gson.JsonSyntaxException; |
| 23 | +import com.google.gson.TypeAdapter; |
| 24 | +import com.google.gson.annotations.JsonAdapter; |
| 25 | +import com.google.gson.stream.JsonReader; |
| 26 | +import com.google.gson.stream.JsonToken; |
| 27 | +import com.google.gson.stream.JsonWriter; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Locale; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +/** |
| 33 | + * Response from the {@code get} command of the <a |
| 34 | + * href="https://github.com/bazelbuild/proposals/blob/main/designs/2022-06-07-bazel-credential-helpers.md#proposal">Credential |
| 35 | + * Helper Protocol</a>. |
| 36 | + */ |
| 37 | +@AutoValue |
| 38 | +@AutoValue.CopyAnnotations |
| 39 | +@Immutable |
| 40 | +@JsonAdapter(GetCredentialsResponse.GsonTypeAdapter.class) |
| 41 | +public abstract class GetCredentialsResponse { |
| 42 | + /** Returns the headers to attach to the request. */ |
| 43 | + public abstract ImmutableMap<String, ImmutableList<String>> getHeaders(); |
| 44 | + |
| 45 | + /** Returns a new builder for {@link GetCredentialsRequest}. */ |
| 46 | + public static Builder newBuilder() { |
| 47 | + return new AutoValue_GetCredentialsResponse.Builder(); |
| 48 | + } |
| 49 | + |
| 50 | + /** Builder for {@link GetCredentialsResponse}. */ |
| 51 | + @AutoValue.Builder |
| 52 | + public abstract static class Builder { |
| 53 | + protected abstract ImmutableMap.Builder<String, ImmutableList<String>> headersBuilder(); |
| 54 | + |
| 55 | + /** Returns the newly constructed {@link GetCredentialsResponse}. */ |
| 56 | + public abstract GetCredentialsResponse build(); |
| 57 | + } |
| 58 | + |
| 59 | + /** GSON adapter for GetCredentialsResponse. */ |
| 60 | + public static final class GsonTypeAdapter extends TypeAdapter<GetCredentialsResponse> { |
| 61 | + @Override |
| 62 | + public void write(JsonWriter writer, GetCredentialsResponse response) throws IOException { |
| 63 | + Preconditions.checkNotNull(writer); |
| 64 | + Preconditions.checkNotNull(response); |
| 65 | + |
| 66 | + writer.beginObject(); |
| 67 | + |
| 68 | + ImmutableMap<String, ImmutableList<String>> headers = response.getHeaders(); |
| 69 | + if (!headers.isEmpty()) { |
| 70 | + writer.name("headers"); |
| 71 | + writer.beginObject(); |
| 72 | + for (Map.Entry<String, ImmutableList<String>> entry : headers.entrySet()) { |
| 73 | + writer.name(entry.getKey()); |
| 74 | + |
| 75 | + writer.beginArray(); |
| 76 | + for (String value : entry.getValue()) { |
| 77 | + writer.value(value); |
| 78 | + } |
| 79 | + writer.endArray(); |
| 80 | + } |
| 81 | + writer.endObject(); |
| 82 | + } |
| 83 | + writer.endObject(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public GetCredentialsResponse read(JsonReader reader) throws IOException { |
| 88 | + Preconditions.checkNotNull(reader); |
| 89 | + |
| 90 | + GetCredentialsResponse.Builder response = newBuilder(); |
| 91 | + |
| 92 | + if (reader.peek() != JsonToken.BEGIN_OBJECT) { |
| 93 | + throw new JsonSyntaxException( |
| 94 | + String.format(Locale.US, "Expected object, got %s", reader.peek())); |
| 95 | + } |
| 96 | + reader.beginObject(); |
| 97 | + |
| 98 | + while (reader.hasNext()) { |
| 99 | + String name = reader.nextName(); |
| 100 | + switch (name) { |
| 101 | + case "headers": |
| 102 | + if (reader.peek() != JsonToken.BEGIN_OBJECT) { |
| 103 | + throw new JsonSyntaxException( |
| 104 | + String.format( |
| 105 | + Locale.US, |
| 106 | + "Expected value of 'headers' to be an object, got %s", |
| 107 | + reader.peek())); |
| 108 | + } |
| 109 | + reader.beginObject(); |
| 110 | + |
| 111 | + while (reader.hasNext()) { |
| 112 | + String headerName = reader.nextName(); |
| 113 | + ImmutableList.Builder<String> headerValues = ImmutableList.builder(); |
| 114 | + |
| 115 | + if (reader.peek() != JsonToken.BEGIN_ARRAY) { |
| 116 | + throw new JsonSyntaxException( |
| 117 | + String.format( |
| 118 | + Locale.US, |
| 119 | + "Expected value of '%s' header to be an array of strings, got %s", |
| 120 | + headerName, |
| 121 | + reader.peek())); |
| 122 | + } |
| 123 | + reader.beginArray(); |
| 124 | + for (int i = 0; reader.hasNext(); i++) { |
| 125 | + if (reader.peek() != JsonToken.STRING) { |
| 126 | + throw new JsonSyntaxException( |
| 127 | + String.format( |
| 128 | + Locale.US, |
| 129 | + "Expected value %s of '%s' header to be a string, got %s", |
| 130 | + i, |
| 131 | + headerName, |
| 132 | + reader.peek())); |
| 133 | + } |
| 134 | + headerValues.add(reader.nextString()); |
| 135 | + } |
| 136 | + reader.endArray(); |
| 137 | + |
| 138 | + response.headersBuilder().put(headerName, headerValues.build()); |
| 139 | + } |
| 140 | + |
| 141 | + reader.endObject(); |
| 142 | + break; |
| 143 | + |
| 144 | + default: |
| 145 | + // We intentionally ignore unknown keys to achieve forward compatibility with responses |
| 146 | + // coming from newer tools. |
| 147 | + reader.skipValue(); |
| 148 | + } |
| 149 | + } |
| 150 | + reader.endObject(); |
| 151 | + return response.build(); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments