6
6
import com .uid2 .shared .Utils ;
7
7
import com .uid2 .shared .secure .AttestationClientException ;
8
8
import com .uid2 .shared .secure .AttestationException ;
9
+ import com .uid2 .shared .secure .AttestationFailure ;
10
+ import com .uid2 .shared .util .UrlEquivalenceValidator ;
9
11
import org .apache .commons .collections4 .CollectionUtils ;
10
12
import org .apache .commons .collections4 .MapUtils ;
11
13
import org .slf4j .Logger ;
@@ -30,14 +32,24 @@ public class PolicyValidator implements IPolicyValidator {
30
32
31
33
private static final List <String > REQUIRED_ENV_OVERRIDES = ImmutableList .of (
32
34
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
36
36
);
37
37
38
38
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
+ )
40
47
);
48
+ private final String attestationUrl ;
49
+
50
+ public PolicyValidator (String attestationUrl ) {
51
+ this .attestationUrl = attestationUrl ;
52
+ }
41
53
42
54
@ Override
43
55
public String getVersion () {
@@ -56,18 +68,18 @@ public String validate(TokenPayload payload) throws AttestationException {
56
68
57
69
private static boolean checkConfidentialSpace (TokenPayload payload ) throws AttestationException {
58
70
if (!payload .isConfidentialSpaceSW ()){
59
- throw new AttestationClientException ("Unexpected SW_NAME: " + payload .getSwName ());
71
+ throw new AttestationClientException ("Unexpected SW_NAME: " + payload .getSwName (), AttestationFailure . BAD_FORMAT );
60
72
}
61
73
var isDebugMode = payload .isDebugMode ();
62
74
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 );
64
76
}
65
77
return isDebugMode ;
66
78
}
67
79
68
80
private static String checkWorkload (TokenPayload payload ) throws AttestationException {
69
81
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 );
71
83
}
72
84
return payload .getWorkloadImageDigest ();
73
85
}
@@ -78,35 +90,35 @@ private static String checkWorkload(TokenPayload payload) throws AttestationExce
78
90
private static String checkRegion (TokenPayload payload ) throws AttestationException {
79
91
var region = payload .getGceZone ();
80
92
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 );
82
94
}
83
95
return region ;
84
96
}
85
97
86
98
private static void checkCmdOverrides (TokenPayload payload ) throws AttestationException {
87
99
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 );
89
101
}
90
102
}
91
103
92
104
private Environment checkEnvOverrides (TokenPayload payload ) throws AttestationException {
93
105
var envOverrides = payload .getEnvOverrides ();
94
106
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 );
96
108
}
97
109
HashMap <String , String > envOverridesCopy = new HashMap (envOverrides );
98
110
99
111
// check all required env overrides
100
112
for (var envKey : REQUIRED_ENV_OVERRIDES ){
101
113
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 );
103
115
}
104
116
}
105
117
106
118
// env could be parsed
107
119
var env = Environment .fromString (envOverridesCopy .get (ENV_ENVIRONMENT ));
108
120
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 );
110
122
}
111
123
112
124
// make sure there's no unexpected overrides
@@ -120,13 +132,24 @@ private Environment checkEnvOverrides(TokenPayload payload) throws AttestationEx
120
132
}
121
133
}
122
134
135
+ checkAttestationUrl (new HashMap <>(envOverrides ));
136
+
123
137
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 );
125
139
}
126
140
127
141
return env ;
128
142
}
129
143
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
+
130
153
private String generateEnclaveId (boolean isDebugMode , String imageDigest , Environment env ) throws AttestationException {
131
154
var str = String .format ("%s,%s,%s" , getVersion (), isDebugMode , imageDigest );
132
155
LOGGER .info ("Meta used to generate GCP EnclaveId: " + str );
0 commit comments