66import com .uid2 .shared .Utils ;
77import com .uid2 .shared .secure .AttestationClientException ;
88import com .uid2 .shared .secure .AttestationException ;
9+ import com .uid2 .shared .secure .AttestationFailure ;
10+ import com .uid2 .shared .util .UrlEquivalenceValidator ;
911import org .apache .commons .collections4 .CollectionUtils ;
1012import org .apache .commons .collections4 .MapUtils ;
1113import org .slf4j .Logger ;
@@ -30,14 +32,24 @@ public class PolicyValidator implements IPolicyValidator {
3032
3133 private static final List <String > REQUIRED_ENV_OVERRIDES = ImmutableList .of (
3234 ENV_ENVIRONMENT ,
33- ENV_OPERATOR_API_KEY_SECRET_NAME ,
34- ENV_CORE_ENDPOINT ,
35- ENV_OPT_OUT_ENDPOINT
35+ ENV_OPERATOR_API_KEY_SECRET_NAME
3636 );
3737
3838 private static final Map <Environment , List <String >> OPTIONAL_ENV_OVERRIDES_MAP = ImmutableMap .of (
39- Environment .Integration , ImmutableList .of ()
39+ Environment .Production , ImmutableList .of (
40+ ENV_CORE_ENDPOINT ,
41+ ENV_OPT_OUT_ENDPOINT
42+ ),
43+ Environment .Integration , ImmutableList .of (
44+ ENV_CORE_ENDPOINT ,
45+ ENV_OPT_OUT_ENDPOINT
46+ )
4047 );
48+ private final String attestationUrl ;
49+
50+ public PolicyValidator (String attestationUrl ) {
51+ this .attestationUrl = attestationUrl ;
52+ }
4153
4254 @ Override
4355 public String getVersion () {
@@ -56,18 +68,18 @@ public String validate(TokenPayload payload) throws AttestationException {
5668
5769 private static boolean checkConfidentialSpace (TokenPayload payload ) throws AttestationException {
5870 if (!payload .isConfidentialSpaceSW ()){
59- throw new AttestationClientException ("Unexpected SW_NAME: " + payload .getSwName ());
71+ throw new AttestationClientException ("Unexpected SW_NAME: " + payload .getSwName (), AttestationFailure . BAD_FORMAT );
6072 }
6173 var isDebugMode = payload .isDebugMode ();
6274 if (!isDebugMode && !payload .isStableVersion ()){
63- throw new AttestationClientException ("Confidential space image version is not stable." );
75+ throw new AttestationClientException ("Confidential space image version is not stable." , AttestationFailure . BAD_FORMAT );
6476 }
6577 return isDebugMode ;
6678 }
6779
6880 private static String checkWorkload (TokenPayload payload ) throws AttestationException {
6981 if (!payload .isRestartPolicyNever ()){
70- throw new AttestationClientException ("Restart policy is not set to Never. Value: " + payload .getRestartPolicy ());
82+ throw new AttestationClientException ("Restart policy is not set to Never. Value: " + payload .getRestartPolicy (), AttestationFailure . BAD_FORMAT );
7183 }
7284 return payload .getWorkloadImageDigest ();
7385 }
@@ -78,35 +90,35 @@ private static String checkWorkload(TokenPayload payload) throws AttestationExce
7890 private static String checkRegion (TokenPayload payload ) throws AttestationException {
7991 var region = payload .getGceZone ();
8092 if (Strings .isNullOrEmpty (region ) || region .startsWith (EU_REGION_PREFIX )){
81- throw new AttestationClientException ("Region is not supported. Value: " + region );
93+ throw new AttestationClientException ("Region is not supported. Value: " + region , AttestationFailure . BAD_FORMAT );
8294 }
8395 return region ;
8496 }
8597
8698 private static void checkCmdOverrides (TokenPayload payload ) throws AttestationException {
8799 if (!CollectionUtils .isEmpty (payload .getCmdOverrides ())){
88- throw new AttestationClientException ("Payload should not have cmd overrides" );
100+ throw new AttestationClientException ("Payload should not have cmd overrides" , AttestationFailure . BAD_FORMAT );
89101 }
90102 }
91103
92104 private Environment checkEnvOverrides (TokenPayload payload ) throws AttestationException {
93105 var envOverrides = payload .getEnvOverrides ();
94106 if (MapUtils .isEmpty (envOverrides )){
95- throw new AttestationClientException ("env overrides should not be empty" );
107+ throw new AttestationClientException ("env overrides should not be empty" , AttestationFailure . BAD_FORMAT );
96108 }
97109 HashMap <String , String > envOverridesCopy = new HashMap (envOverrides );
98110
99111 // check all required env overrides
100112 for (var envKey : REQUIRED_ENV_OVERRIDES ){
101113 if (Strings .isNullOrEmpty (envOverridesCopy .get (envKey ))){
102- throw new AttestationClientException ("Required env override is missing. key: " + envKey );
114+ throw new AttestationClientException ("Required env override is missing. key: " + envKey , AttestationFailure . BAD_FORMAT );
103115 }
104116 }
105117
106118 // env could be parsed
107119 var env = Environment .fromString (envOverridesCopy .get (ENV_ENVIRONMENT ));
108120 if (env == null ){
109- throw new AttestationClientException ("Environment can not be parsed. " + envOverridesCopy .get (ENV_ENVIRONMENT ));
121+ throw new AttestationClientException ("Environment can not be parsed. " + envOverridesCopy .get (ENV_ENVIRONMENT ), AttestationFailure . BAD_FORMAT );
110122 }
111123
112124 // make sure there's no unexpected overrides
@@ -120,13 +132,24 @@ private Environment checkEnvOverrides(TokenPayload payload) throws AttestationEx
120132 }
121133 }
122134
135+ checkAttestationUrl (new HashMap <>(envOverrides ));
136+
123137 if (!envOverridesCopy .isEmpty ()){
124- throw new AttestationClientException ("More env overrides than allowed. " + envOverridesCopy );
138+ throw new AttestationClientException ("More env overrides than allowed. " + envOverridesCopy , AttestationFailure . BAD_FORMAT );
125139 }
126140
127141 return env ;
128142 }
129143
144+ private void checkAttestationUrl (HashMap <String , String > optionalEnvOverrides ) throws AttestationException {
145+ if (!Strings .isNullOrEmpty (optionalEnvOverrides .get (ENV_CORE_ENDPOINT ))) {
146+ String givenAttestationUrl = optionalEnvOverrides .get (ENV_CORE_ENDPOINT );
147+ if (!UrlEquivalenceValidator .areUrlsEquivalent (givenAttestationUrl , this .attestationUrl )) {
148+ throw new AttestationClientException ("The given attestation URL is unknown. Given URL: " + givenAttestationUrl , AttestationFailure .UNKNOWN_ATTESTATION_URL );
149+ }
150+ }
151+ }
152+
130153 private String generateEnclaveId (boolean isDebugMode , String imageDigest , Environment env ) throws AttestationException {
131154 var str = String .format ("%s,%s,%s" , getVersion (), isDebugMode , imageDigest );
132155 LOGGER .info ("Meta used to generate GCP EnclaveId: " + str );
0 commit comments