-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from sigstore/signer-select-id
Allow signers to specify allow list of oidc ids
- Loading branch information
Showing
8 changed files
with
201 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sigstore-java/src/main/java/dev/sigstore/KeylessSignerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2022 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; | ||
|
||
public class KeylessSignerException extends Exception { | ||
public KeylessSignerException(String message) { | ||
super(message); | ||
} | ||
|
||
public KeylessSignerException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public KeylessSignerException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
sigstore-java/src/main/java/dev/sigstore/OidcIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2023 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; | ||
|
||
import dev.sigstore.oidc.client.OidcToken; | ||
import org.immutables.value.Value.Immutable; | ||
|
||
@Immutable | ||
public interface OidcIdentity { | ||
|
||
static OidcIdentity of(String identity, String issuer) { | ||
return ImmutableOidcIdentity.builder().identity(identity).issuer(issuer).build(); | ||
} | ||
|
||
static OidcIdentity from(OidcToken oidcToken) { | ||
return ImmutableOidcIdentity.builder() | ||
.identity(oidcToken.getSubjectAlternativeName()) | ||
.issuer(oidcToken.getIssuer()) | ||
.build(); | ||
} | ||
|
||
/** The user or machineId. */ | ||
String getIdentity(); | ||
|
||
/** The oidc issuing authority */ | ||
String getIssuer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,23 @@ | |
*/ | ||
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.oidc.client.GithubActionsOidcClient; | ||
import dev.sigstore.testing.matchers.ByteArrayListMatcher; | ||
import dev.sigstore.testkit.annotations.EnabledIfOidcExists; | ||
import dev.sigstore.testkit.annotations.OidcProviderType; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import org.bouncycastle.util.encoders.Hex; | ||
import org.hamcrest.CoreMatchers; | ||
import org.hamcrest.MatcherAssert; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -92,4 +100,49 @@ public void sign_files() throws Exception { | |
public void sign_digest() throws Exception { | ||
Assertions.assertEquals(signingResults.get(0), signer.sign(artifactHashes.get(0))); | ||
} | ||
|
||
@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"))) | ||
.build(); | ||
var ex = | ||
Assertions.assertThrows( | ||
KeylessSignerException.class, | ||
() -> | ||
signer.sign( | ||
Hex.decode( | ||
"10f26b52447ec6427c178cadb522ce649922ee67f6d59709e45700aa5df68b30"))); | ||
MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.startsWith("Obtained Oidc Token")); | ||
MatcherAssert.assertThat( | ||
ex.getMessage(), CoreMatchers.endsWith("does not match any identities in allow list")); | ||
} | ||
|
||
@Test | ||
@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().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"))) | ||
.build(); | ||
Assertions.assertDoesNotThrow( | ||
() -> | ||
signer.sign( | ||
Hex.decode("10f26b52447ec6427c178cadb522ce649922ee67f6d59709e45700aa5df68b30"))); | ||
} | ||
} |