Skip to content

Commit

Permalink
Apply StringMatcher to OidcIdentity
Browse files Browse the repository at this point in the history
Signed-off-by: Appu Goundan <[email protected]>
  • Loading branch information
loosebazooka committed Jun 10, 2024
1 parent 5113aa2 commit 74be013
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
7 changes: 2 additions & 5 deletions sigstore-java/src/main/java/dev/sigstore/KeylessSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,9 @@ private void renewSigningCertificate()

// check if we have an allow list and if so, ensure the provided token is in there
if (!oidcIdentities.isEmpty()) {
var obtainedToken = OidcIdentity.from(tokenInfo);
if (!oidcIdentities.contains(OidcIdentity.from(tokenInfo))) {
if (oidcIdentities.stream().noneMatch(id -> id.matches(tokenInfo))) {
throw new KeylessSignerException(
"Obtained Oidc Token "
+ obtainedToken
+ " does not match any identities in allow list");
"Obtained Oidc Token " + tokenInfo + " does not match any identities in allow list");
}
}

Expand Down
17 changes: 9 additions & 8 deletions sigstore-java/src/main/java/dev/sigstore/OidcIdentity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@
package dev.sigstore;

import dev.sigstore.oidc.client.OidcToken;
import dev.sigstore.strings.StringMatcher;
import org.immutables.value.Value.Default;
import org.immutables.value.Value.Immutable;

@Immutable
public interface OidcIdentity {

static OidcIdentity of(String identity, String issuer) {
static OidcIdentity of(StringMatcher identity, StringMatcher issuer) {
return ImmutableOidcIdentity.builder().identity(identity).issuer(issuer).build();
}

static OidcIdentity from(OidcToken oidcToken) {
return ImmutableOidcIdentity.builder()
.identity(oidcToken.getSubjectAlternativeName())
.issuer(oidcToken.getIssuer())
.build();
@Default
default boolean matches(OidcToken oidcToken) {
return getIdentity().test(oidcToken.getSubjectAlternativeName())
&& getIssuer().test(oidcToken.getIssuer());
}

/** The user or machineId. */
String getIdentity();
StringMatcher getIdentity();

/** The oidc issuing authority */
String getIssuer();
StringMatcher getIssuer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ public interface OidcToken {
String getIssuer();

/** The full oidc token obtained from the provider. */
@Value.Redacted
String getIdToken();
}
25 changes: 12 additions & 13 deletions sigstore-java/src/test/java/dev/sigstore/KeylessSignerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package dev.sigstore;

import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.common.hash.Hashing;
import dev.sigstore.bundle.Bundle;
import dev.sigstore.oidc.client.GithubActionsOidcClient;
import dev.sigstore.strings.StringMatcher;
import dev.sigstore.testing.matchers.ByteArrayListMatcher;
import dev.sigstore.testkit.annotations.EnabledIfOidcExists;
import dev.sigstore.testkit.annotations.OidcProviderType;
Expand Down Expand Up @@ -104,12 +102,15 @@ public void sign_digest() throws Exception {

@Test
@EnabledIfOidcExists(provider = OidcProviderType.GITHUB)
// this test will only pass on the github.com/sigstore/sigstore-java repository
public void sign_failGithubOidcCheck() throws Exception {
var signer =
KeylessSigner.builder()
.sigstorePublicDefaults()
.allowedOidcIdentities(List.of(OidcIdentity.of("[email protected]", "goose.com")))
.allowedOidcIdentities(
List.of(
OidcIdentity.of(
StringMatcher.string("[email protected]"),
StringMatcher.string("goose.com"))))
.build();
var ex =
Assertions.assertThrows(
Expand All @@ -127,20 +128,18 @@ public void sign_failGithubOidcCheck() throws Exception {
@EnabledIfOidcExists(provider = OidcProviderType.GITHUB)
// this test will only pass on the github.com/sigstore/sigstore-java repository
public void sign_passGithubOidcCheck() throws Exception {
// silly way to get the right oidc identity to make sure our simple matcher works
var jws =
JsonWebSignature.parse(
new GsonFactory(),
GithubActionsOidcClient.builder().build().getIDToken(System.getenv()).getIdToken());
var expectedGithubSubject = jws.getPayload().getSubject();
var signer =
KeylessSigner.builder()
.sigstorePublicDefaults()
.allowedOidcIdentities(
List.of(
OidcIdentity.of(
expectedGithubSubject, "https://token.actions.githubusercontent.com"),
OidcIdentity.of("[email protected]", "https://accounts.other.com")))
StringMatcher.regex(
"https://github\\.com/sigstore/sigstore-java/\\.github/workflows/.*\\.yaml@.*"),
StringMatcher.string("https://token.actions.githubusercontent.com")),
OidcIdentity.of(
StringMatcher.string("[email protected]"),
StringMatcher.string("https://accounts.other.com"))))
.build();
Assertions.assertDoesNotThrow(
() ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 The Sigstore Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.sigstore.oidc.client;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class OidcTokenTest {

@Test
public void test_redacted() {
var testToken =
ImmutableOidcToken.builder()
.issuer("issuer")
.idToken("secret")
.subjectAlternativeName("name")
.build();
Assertions.assertEquals(
"OidcToken{subjectAlternativeName=name, issuer=issuer}", testToken.toString());
}
}

0 comments on commit 74be013

Please sign in to comment.