12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import os
15
16
from pathlib import Path
16
17
from typing import Tuple
17
18
18
19
import pytest
19
20
21
+ from sigstore ._internal .oidc .ambient import (
22
+ AmbientCredentialError ,
23
+ GitHubOidcPermissionCredentialError ,
24
+ detect_credential ,
25
+ )
26
+
20
27
_ASSETS = (Path (__file__ ).parent / "assets" ).resolve ()
21
28
assert _ASSETS .is_dir ()
22
29
23
30
31
+ def _is_ambient_env ():
32
+ try :
33
+ token = detect_credential ()
34
+ if token is None :
35
+ return False
36
+ except GitHubOidcPermissionCredentialError :
37
+ # On GitHub Actions, forks do not have access to OIDC identities.
38
+ # We differentiate this case from other GitHub credential errors,
39
+ # since it's a case where we want to skip (i.e. return False).
40
+ if os .getenv ("GITHUB_EVENT_NAME" ) == "pull_request" :
41
+ return False
42
+ return True
43
+ except AmbientCredentialError :
44
+ # If ambient credential detection raises, then we *are* in an ambient
45
+ # environment but one that's been configured incorrectly. We
46
+ # pass this through, so that the CI fails appropriately rather than
47
+ # silently skipping the faulty tests.
48
+ return True
49
+
50
+ return True
51
+
52
+
24
53
def pytest_addoption (parser ):
25
54
parser .addoption (
26
55
"--skip-online" ,
@@ -34,12 +63,17 @@ def pytest_runtest_setup(item):
34
63
pytest .skip (
35
64
"skipping test that requires network connectivity due to `--skip-online` flag"
36
65
)
66
+ elif "ambient_oidc" in item .keywords and not _is_ambient_env ():
67
+ pytest .skip ("skipping test that requires an ambient OIDC credential" )
37
68
38
69
39
70
def pytest_configure (config ):
40
71
config .addinivalue_line (
41
72
"markers" , "online: mark test as requiring network connectivity"
42
73
)
74
+ config .addinivalue_line (
75
+ "markers" , "ambient_oidc: mark test as requiring an ambient OIDC identity"
76
+ )
43
77
44
78
45
79
@pytest .fixture
0 commit comments