Skip to content

Commit 53b4ee3

Browse files
hsudhofcopybara-github
authored andcommitted
Generalize auth to be used in other contexts
This is largely a rename with a slightly widened interface. BUG=275368350 PiperOrigin-RevId: 560174530 Change-Id: I680b462564f98a8bb03e97d89c330d6ce6870e8f
1 parent 3c4fc21 commit 53b4ee3

32 files changed

+875
-171
lines changed

java/com/google/copybara/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ java_library(
230230
"//java/com/google/copybara/config:base",
231231
"//java/com/google/copybara/config:global_migrations",
232232
"//java/com/google/copybara/config:parser",
233+
"//java/com/google/copybara/credentials",
233234
"//java/com/google/copybara/doc:annotations",
234235
"//java/com/google/copybara/effect",
235236
"//java/com/google/copybara/exception",

java/com/google/copybara/ConfigItemDescription.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableSetMultimap;
2020
import com.google.copybara.util.Glob;
21+
import javax.annotation.Nullable;
2122

2223
/**
2324
* Interface for self-description. The information returned should be sufficient to create a new
@@ -31,7 +32,7 @@ default String getType() {
3132
}
3233

3334
/** Returns a key-value ist of the options the endpoint was instantiated with. */
34-
default ImmutableSetMultimap<String, String> describe(Glob originFiles) {
35+
default ImmutableSetMultimap<String, String> describe(@Nullable Glob originFiles) {
3536
ImmutableSetMultimap.Builder<String, String> builder =
3637
new ImmutableSetMultimap.Builder<String, String>()
3738
.put("type", getType());

java/com/google/copybara/Endpoint.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.copybara;
1818

19+
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.ImmutableSetMultimap;
2021
import com.google.copybara.effect.DestinationEffect.DestinationRef;
2122
import com.google.copybara.revision.OriginRef;
@@ -63,9 +64,14 @@ default void repr(Printer printer) {
6364
printer.append(toString());
6465
}
6566

66-
/** Returns a key-value ist of the options the endpoint was instantiated with. */
67+
/** Returns a key-value list of the options the endpoint was instantiated with. */
6768
ImmutableSetMultimap<String, String> describe();
6869

70+
/** Returns a key-value list describing the credentials the endpoint was instantiated with. */
71+
default ImmutableList<ImmutableSetMultimap<String, String>> describeCredentials() {
72+
return ImmutableList.of();
73+
}
74+
6975
@StarlarkMethod(
7076
name = "new_origin_ref",
7177
doc = "Creates a new origin reference out of this endpoint.",

java/com/google/copybara/ModuleSupplier.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.google.copybara.buildozer.BuildozerModule;
2626
import com.google.copybara.buildozer.BuildozerOptions;
2727
import com.google.copybara.compression.CompressionModule;
28+
import com.google.copybara.credentials.CredentialModule;
29+
import com.google.copybara.credentials.CredentialOptions;
2830
import com.google.copybara.folder.FolderDestinationOptions;
2931
import com.google.copybara.folder.FolderModule;
3032
import com.google.copybara.folder.FolderOriginOptions;
@@ -132,6 +134,7 @@ public ImmutableSet<Object> getModules(Options options) {
132134
new HttpModule(console, options.get(HttpOptions.class)),
133135
new PythonModule(),
134136
new CompressionModule(),
137+
new CredentialModule(console, options.get(CredentialOptions.class)),
135138
Json.INSTANCE);
136139
}
137140

@@ -166,7 +169,8 @@ protected Options newOptions() {
166169
new DebugOptions(generalOptions),
167170
new GeneratorOptions(),
168171
new HttpOptions(),
169-
new RegenerateOptions()));
172+
new RegenerateOptions(),
173+
new CredentialOptions()));
170174
}
171175

172176
/**
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2023 Google LLC.
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(default_visibility = ["//visibility:public"])
16+
17+
licenses(["notice"])
18+
19+
java_library(
20+
name = "credentials",
21+
srcs = glob(["*.java"]),
22+
deps = [
23+
"//java/com/google/copybara:options",
24+
"//java/com/google/copybara/exception",
25+
"//java/com/google/copybara/util/console",
26+
"//third_party:autovalue",
27+
"//third_party:guava",
28+
"//third_party:jsr305",
29+
"//third_party:starlark",
30+
"//third_party:tomlj",
31+
],
32+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.common.base.Preconditions;
19+
import com.google.common.collect.ImmutableSetMultimap;
20+
21+
/**
22+
* A static CredentialIssuer, e.g. a password, username, api key, etc
23+
*/
24+
public class ConstantCredentialIssuer implements CredentialIssuer {
25+
26+
private final String secret;
27+
private final String name;
28+
29+
private final boolean open;
30+
31+
public static ConstantCredentialIssuer createConstantSecret(String name, String secret) {
32+
return new ConstantCredentialIssuer(
33+
Preconditions.checkNotNull(name), Preconditions.checkNotNull(secret), false);
34+
}
35+
36+
public static ConstantCredentialIssuer createConstantOpenValue(String value) {
37+
return new ConstantCredentialIssuer(Preconditions.checkNotNull(value), value, true);
38+
}
39+
40+
private ConstantCredentialIssuer(String name, String secret, boolean open) {
41+
this.secret = secret;
42+
this.name = name;
43+
this.open = open;
44+
}
45+
46+
@Override
47+
public Credential issue() throws CredentialIssuingException {
48+
return open ? new OpenCredential(secret) : new StaticSecret(name, secret);
49+
}
50+
51+
@Override
52+
public ImmutableSetMultimap<String, String> describe() {
53+
return ImmutableSetMultimap.of("type", "constant", "name", name, "open", "" + open);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/** Holder for a credential. */
19+
public interface Credential {
20+
21+
/** A safe value that describes the credential */
22+
String printableValue();
23+
24+
/** Whether the creential is still believed to be valid */
25+
boolean valid();
26+
27+
/** The raw secret, this should not be used outside of framework code. */
28+
String provideSecret() throws CredentialRetrievalException;
29+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023 Google Inc.
2+
* Copyright (C) 2023 Google LLC.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,24 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
package com.google.copybara.credentials;
1617

17-
package com.google.copybara.http.auth;
18+
import com.google.common.collect.ImmutableSetMultimap;
19+
import net.starlark.java.eval.StarlarkValue;
1820

19-
import java.io.IOException;
21+
/**
22+
* An object able to mint credentials. The issuer should handle caching etc.
23+
*/
24+
public interface CredentialIssuer extends StarlarkValue {
2025

21-
/** provider for auth credentials */
22-
public interface KeySource {
23-
String get() throws IOException;
26+
/**
27+
* Issue a Credential to be used by an endpoint
28+
*/
29+
Credential issue() throws CredentialIssuingException;
2430

2531
/**
26-
* Signifies the key source was unable to locate the key
27-
* it is attempting to get.
28-
* Extends IOException to work more easily with the
29-
* http client interceptor type signature.
32+
* Metadata describing this issuer.
3033
*/
31-
class KeyNotFoundException extends IOException {
32-
public KeyNotFoundException(String message) {
33-
super(message);
34-
}
35-
}
34+
ImmutableSetMultimap<String, String> describe();
3635
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.copybara.exception.ValidationException;
19+
20+
/**
21+
* An Exception thrown if minting a credential fails.
22+
*/
23+
public class CredentialIssuingException extends ValidationException {
24+
25+
public CredentialIssuingException(String message) {
26+
super(message);
27+
}
28+
29+
public CredentialIssuingException(String message, Throwable cause) {
30+
super(message, cause);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)