|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Google LLC. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.copybara.credentials; |
| 17 | + |
| 18 | +import com.google.auto.value.AutoValue; |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import com.google.common.collect.ImmutableSetMultimap; |
| 21 | +import com.google.copybara.exception.ValidationException; |
| 22 | +import com.google.copybara.util.console.Console; |
| 23 | +import net.starlark.java.annot.Param; |
| 24 | +import net.starlark.java.annot.ParamType; |
| 25 | +import net.starlark.java.annot.StarlarkBuiltin; |
| 26 | +import net.starlark.java.annot.StarlarkMethod; |
| 27 | +import net.starlark.java.eval.EvalException; |
| 28 | +import net.starlark.java.eval.StarlarkValue; |
| 29 | + |
| 30 | +/** Starlark builtins to handle credentials. */ |
| 31 | +@StarlarkBuiltin(name = "credentials", doc = "Module for working with credentials.") |
| 32 | +public class CredentialModule implements StarlarkValue { |
| 33 | + |
| 34 | + protected CredentialOptions options; |
| 35 | + protected Console console; |
| 36 | + |
| 37 | + public CredentialModule(Console console, CredentialOptions options) { |
| 38 | + this.console = console; |
| 39 | + this.options = options; |
| 40 | + } |
| 41 | + |
| 42 | + @StarlarkMethod( |
| 43 | + name = "static_secret", |
| 44 | + doc = "Holder for secrets that can be in plaintext within the config.", |
| 45 | + parameters = { |
| 46 | + @Param(name = "name", doc = "A name for this secret."), |
| 47 | + @Param(name = "secret", doc = "The secret value.") |
| 48 | + }) |
| 49 | + public CredentialIssuer staticSecret(String name, String secret) throws EvalException { |
| 50 | + return ConstantCredentialIssuer.createConstantSecret(name, secret); |
| 51 | + } |
| 52 | + |
| 53 | + @StarlarkMethod( |
| 54 | + name = "static_value", |
| 55 | + doc = "Holder for credentials that are safe to read/log (e.g. 'x-access-token') .", |
| 56 | + parameters = {@Param(name = "value", doc = "The open value.")}) |
| 57 | + public CredentialIssuer staticValue(String value) throws EvalException { |
| 58 | + return ConstantCredentialIssuer.createConstantOpenValue(value); |
| 59 | + } |
| 60 | + |
| 61 | + @StarlarkMethod( |
| 62 | + name = "toml_key_source", |
| 63 | + doc = |
| 64 | + "Supply an authentication credential from the " |
| 65 | + + "file pointed to by the --http-credential-file flag.", |
| 66 | + parameters = { |
| 67 | + @Param( |
| 68 | + name = "dot_path", |
| 69 | + doc = "Dot path to the data field containing the credential.", |
| 70 | + allowedTypes = {@ParamType(type = String.class)}) |
| 71 | + }) |
| 72 | + public CredentialIssuer tomlKeySource(String dotPath) throws ValidationException { |
| 73 | + if (options.credentialFile == null) { |
| 74 | + throw new ValidationException("Credential file for toml key source has not been supplied"); |
| 75 | + } |
| 76 | + return new TomlKeySource(options.credentialFile, dotPath); |
| 77 | + } |
| 78 | + |
| 79 | + @StarlarkMethod( |
| 80 | + name = "username_password", |
| 81 | + doc = "A pair of username and password credential issuers.", |
| 82 | + parameters = { |
| 83 | + @Param( |
| 84 | + name = "username", |
| 85 | + doc = "Username credential.", |
| 86 | + allowedTypes = {@ParamType(type = CredentialIssuer.class)}), |
| 87 | + @Param( |
| 88 | + name = "password", |
| 89 | + doc = "Password credential.", |
| 90 | + allowedTypes = {@ParamType(type = CredentialIssuer.class)}) |
| 91 | + }) |
| 92 | + public UsernamePasswordIssuer usernamePassword( |
| 93 | + CredentialIssuer username, CredentialIssuer password) { |
| 94 | + return new AutoValue_CredentialModule_UsernamePasswordIssuer(username, password); |
| 95 | + } |
| 96 | + |
| 97 | + /** A username/password pair issuer */ |
| 98 | + @AutoValue |
| 99 | + public abstract static class UsernamePasswordIssuer implements StarlarkValue { |
| 100 | + public abstract CredentialIssuer username(); |
| 101 | + |
| 102 | + public abstract CredentialIssuer password(); |
| 103 | + |
| 104 | + public ImmutableList<ImmutableSetMultimap<String, String>> describeCredentials() { |
| 105 | + return ImmutableList.of(username().describe(), password().describe()); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments