Skip to content

Commit

Permalink
[uss_qualifier] DSS0030 token validation scenario (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shastick authored Nov 14, 2023
1 parent ab3b59f commit 6409160
Show file tree
Hide file tree
Showing 18 changed files with 1,015 additions and 25 deletions.
68 changes: 66 additions & 2 deletions monitoring/monitorlib/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import datetime
import hashlib
import re
from typing import Any, Dict, List, Optional, Tuple
import urllib.parse
import uuid
from typing import Any, Dict, List, Optional, Tuple

import cryptography.exceptions
import cryptography.hazmat.backends
Expand All @@ -20,6 +20,7 @@
from google.auth import impersonated_credentials
from google.auth.transport import requests as google_requests
from google.oauth2 import service_account

from monitoring.monitorlib.infrastructure import AuthAdapter, AuthSpec

_UNIX_EPOCH = datetime.datetime.utcfromtimestamp(0)
Expand Down Expand Up @@ -82,6 +83,70 @@ def issue_token(self, intended_audience: str, scopes: List[str]) -> str:
return jwt.serialize()


class InvalidTokenSignatureAuth(AuthAdapter):
"""Auth adapter that generates well-formed tokens that are signed with an unknown private key.
The generated tokens are not expected to be accepted,
and should be used in scenarios that test the authentication logic.
"""

# A random RSA2048 private key
unknown_private_key = jwcrypto.jwk.JWK.from_pem(
"-----BEGIN PRIVATE KEY-----\n"
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCsa9ZYfRoeA1A5\n"
"oUY38payXpOQzjFUVmm2CUh2WxY1HV3d2/0nkWcjHSD5wIKRCqhBQ3T3rj1AJm4f\n"
"mUasH0dAurutgggTDTxpX4XiskYdG8NuZfyQxtRMGBivFnbySi3A0FwOWZPW3ZXz\n"
"RC2r27URC1IvZc2cpSkbNngK9OUodbxX/pbFU6ltkPbyztcLgdnAcC/R7JfUUmgm\n"
"kmBg9ZTyCFB1gsX3Bgx2YSBGyjLejfTUBcySoJuYFPobSKxBpO1r3S0XWCnz4WOu\n"
"ho5asoqy23ucI5VXXcOSaNVIBVnJVhFyCum04m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vukcm+RzKu+ojAU5LWXTAt/fOiyXH8JFvuaOw6kiwqNsTps//ZGdZuf9M1qjO/Ge\n"
"jNK98EtuzFFHlESPRUvPv5I5RVg7hU4GWjh0NsMCgYAqVoB5x4+ugae+eZgSLfwG\n"
"YWSOiHQhEqqLWAp3MHbuwDVNnpy1KNWh7A/f8Hd3xgPKQIGCl74dBQ+6pv08Dxyt\n"
"az+aNXi/CmaEb3v6abaKdF7uNJCpKUnYJ3lpLrrd9HsLbhszIhs1iPbJK38SDEm0\n"
"xgbwpLAv3YW+usS9x8LAPw==\n"
"-----END PRIVATE KEY-----".encode("UTF-8")
)

def __init__(self, sub: str = "uss_unsigned"):
super().__init__()
self.sub = sub

def issue_token(self, intended_audience: str, scopes: List[str]) -> str:
timestamp = int((datetime.datetime.utcnow() - _UNIX_EPOCH).total_seconds())
claims = {
"sub": self.sub,
"client_id": self.sub,
"scope": " ".join(scopes),
"aud": intended_audience,
"nbf": timestamp - 1,
"exp": timestamp + NoAuth.EXPIRATION,
"iss": "NoAuth",
"jti": str(uuid.uuid4()),
}
jwt = jwcrypto.jwt.JWT(
header={"typ": "JWT", "alg": "RS256"},
claims=claims,
algs=["RS256"],
)
jwt.make_signed_token(InvalidTokenSignatureAuth.unknown_private_key)
return jwt.serialize()


class DummyOAuth(AuthAdapter):
"""Auth adapter that gets JWTs that uses the Dummy OAuth Server"""

Expand Down Expand Up @@ -490,7 +555,6 @@ def __init__(
client_secret: str,
send_request_as_data: str = "true",
):

send_request_as_data = send_request_as_data.lower() == "true"

super(FlightPassport, self).__init__(
Expand Down
2 changes: 1 addition & 1 deletion monitoring/prober/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def wrapper_default_scope(*args, **kwargs):
resource_type_code_descriptions: Dict[ResourceType, str] = {}


# Next code: 372
# Next code: 373
def register_resource_type(code: int, description: str) -> ResourceType:
"""Register that the specified code refers to the described resource.
Expand Down
Loading

0 comments on commit 6409160

Please sign in to comment.