Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @EnabledIfOidcExists instead of several Gradle test tasks #147

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions .github/workflows/ci-oidc.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
fail-fast: false

runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v3
- name: Set up JDK ${{ matrix.java-version }}
Expand Down
12 changes: 9 additions & 3 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
## build and test
```
```sh
./gradlew build
```

## apply automatic formatting
## skip tests that require OIDC logins

```sh
./gradlew build -PskipOidc
```

## apply automatic formatting
```sh
./gradlew spotlessApply
```

## install into local maven repo
```
```sh
./gradlew publishToMavenLocal
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import buildlogic.filterEolSimple
plugins {
`java-base`
id("com.github.vlsi.gradle-extensions")
id("build-logic.testing")
}

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java-library")
id("build-logic.java")
id("build-logic.testing")
id("com.github.vlsi.gradle-extensions")
id("com.github.autostyle")
kotlin("jvm")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tasks.withType<Test>().configureEach {
if (project.hasProperty("skipOidc")) {
println("skipOidc: ${project.findProperty("skipOidc")}")
systemProperty("sigstore-java.test.skipOidc", project.findProperty("skipOidc")!!)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package dev.sigstore.gradle

import dev.sigstore.testkit.BaseGradleTest
import dev.sigstore.testkit.DisableIfCiWithoutOidc
import dev.sigstore.testkit.annotations.EnabledIfOidcExists
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

@DisableIfCiWithoutOidc
@EnabledIfOidcExists
class SigstoreSignTest: BaseGradleTest() {
@ParameterizedTest
@MethodSource("gradleVersionAndSettings")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package dev.sigstore.gradle

import dev.sigstore.testkit.BaseGradleTest
import dev.sigstore.testkit.DisableIfCiWithoutOidc
import org.assertj.core.api.AbstractCharSequenceAssert
import dev.sigstore.testkit.annotations.EnabledIfOidcExists
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

@DisableIfCiWithoutOidc
@EnabledIfOidcExists
class SigstorePublishSignTest : BaseGradleTest() {
@ParameterizedTest
@MethodSource("gradleVersionAndSettings")
Expand Down
17 changes: 2 additions & 15 deletions sigstore-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ dependencies {
testImplementation("net.sourceforge.htmlunit:htmlunit:2.64.0")
testImplementation("org.eclipse.jetty:jetty-server:11.0.11")

testImplementation(project(":sigstore-testkit"))

implementation("javax.validation:validation-api:2.0.1.Final")
}

Expand Down Expand Up @@ -114,20 +116,5 @@ tasks.named("sourcesJar") {

tasks.test {
useJUnitPlatform {
includeTags("none()")
}
}

// a special test grouping for tests that require a valid gha oidc token
val testGithubOidc by tasks.registering(Test::class) {
useJUnitPlatform {
includeTags("github_oidc")
}
}

// manual test groups that are *not* run in CI, these should be run before
val testManual by tasks.registering(Test::class) {
useJUnitPlatform {
includeTags("manual")
}
}
11 changes: 6 additions & 5 deletions sigstore-java/src/test/java/dev/sigstore/KeylessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import dev.sigstore.oidc.client.GithubActionsOidcClient;
import dev.sigstore.rekor.client.RekorTypeException;
import dev.sigstore.rekor.client.RekorTypes;
import dev.sigstore.testkit.annotations.EnabledIfOidcExists;
import dev.sigstore.testkit.annotations.OidcProviderType;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -29,7 +31,6 @@
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand All @@ -51,7 +52,7 @@ public static void setupArtifact() throws IOException {
}

@Test
@Tag("manual")
@EnabledIfOidcExists(provider = OidcProviderType.MANUAL)
public void sign_production() throws Exception {
var signer = KeylessSigner.builder().sigstorePublicDefaults().build();
var result = signer.sign(testArtifact);
Expand All @@ -66,7 +67,7 @@ public void sign_production() throws Exception {
}

@Test
@Tag("manual")
@EnabledIfOidcExists(provider = OidcProviderType.MANUAL)
public void sign_staging() throws Exception {
var signer = KeylessSigner.builder().sigstoreStagingDefaults().build();
var result = signer.sign(testArtifact);
Expand All @@ -80,7 +81,7 @@ public void sign_staging() throws Exception {
}

@Test
@Tag("github_oidc")
@EnabledIfOidcExists(provider = OidcProviderType.GITHUB)
public void sign_productionWithGithubOidc() throws Exception {
var signer =
KeylessSigner.builder()
Expand All @@ -98,7 +99,7 @@ public void sign_productionWithGithubOidc() throws Exception {
}

@Test
@Tag("github_oidc")
@EnabledIfOidcExists(provider = OidcProviderType.GITHUB)
public void sign_stagingWithGithubOidc() throws Exception {
var signer =
KeylessSigner.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package dev.sigstore.oidc.client;

import dev.sigstore.testing.annotations.RequiresGithubOidc;
import dev.sigstore.testkit.annotations.EnabledIfOidcExists;
import dev.sigstore.testkit.annotations.OidcProviderType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class GithubActionsOidcClientTest {

@Test
@RequiresGithubOidc
@EnabledIfOidcExists(provider = OidcProviderType.GITHUB)
public void getToken() throws OidcException {
var client = GithubActionsOidcClient.builder().build();
var token = client.getIDToken();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.testkit

import dev.sigstore.testkit.annotations.EnabledIfOidcExists
import dev.sigstore.testkit.annotations.OidcProviderType
import org.junit.jupiter.api.extension.ConditionEvaluationResult
import org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled
import org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled
import org.junit.jupiter.api.extension.ExecutionCondition
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.platform.commons.util.AnnotationUtils

class EnableIfOidcExistsCondition : ExecutionCondition {
override fun evaluateExecutionCondition(context: ExtensionContext): ConditionEvaluationResult {
val element = context.element.orElse(null)
val provider = AnnotationUtils.findAnnotation(element, EnabledIfOidcExists::class.java)
.map { it.provider }.orElse(OidcProviderType.ANY)

return when {
provider == OidcProviderType.MANUAL ->
if (System.getenv("CI") == "true") {
disabled("CI environment is present, and the test has been configured to run with MANUAL OIDC only")
} else {
enabled("the test has been configured with MANUAL OIDC, and no CI environment variable is detected")
}

provider in listOf(OidcProviderType.ANY, OidcProviderType.CI, OidcProviderType.GITHUB) &&
System.getenv("ACTIONS_ID_TOKEN_REQUEST_URL") != null ->
enabled("ACTIONS_ID_TOKEN_REQUEST_URL is present, so OIDC matches the requested $provider")

else ->
disabled("test requires $provider OIDC provider")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.testkit.annotations

import dev.sigstore.testkit.EnableIfOidcExistsCondition
import org.junit.jupiter.api.condition.DisabledIfSystemProperty
import org.junit.jupiter.api.extension.ExtendWith

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@DisabledIfSystemProperty(
named = "sigstore-java.test.skipOidc",
matches = "^\\s*+(true|y|on|)\\s*+$",
disabledReason = "sigstore-java.test.skipOidc system property is present",
)
@ExtendWith(EnableIfOidcExistsCondition::class)
annotation class EnabledIfOidcExists(
val provider: OidcProviderType = OidcProviderType.ANY
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@
* 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.testing.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
package dev.sigstore.testkit.annotations

@Target(ElementType.METHOD)
@Tag("github_oidc")
@Retention(RetentionPolicy.RUNTIME)
@EnabledIfEnvironmentVariable(
named = "GITHUB_ACTIONS",
matches = "true",
disabledReason = "test only runs on github actions")
public @interface RequiresGithubOidc {}
enum class OidcProviderType {
ANY,
MANUAL,
CI,
AWS,
AZURE,
GITHUB,
}