17
17
package com .netflix .spinnaker .fiat .shared ;
18
18
19
19
import com .netflix .spinnaker .fiat .model .Authorization ;
20
+ import com .netflix .spinnaker .fiat .model .UserPermission ;
21
+ import com .netflix .spinnaker .fiat .model .resources .Authorizable ;
20
22
import com .netflix .spinnaker .fiat .model .resources .Resource ;
21
23
import lombok .Setter ;
22
24
import lombok .extern .slf4j .Slf4j ;
23
25
import org .springframework .beans .factory .annotation .Autowired ;
24
26
import org .springframework .beans .factory .annotation .Value ;
25
27
import org .springframework .security .access .PermissionEvaluator ;
26
28
import org .springframework .security .core .Authentication ;
29
+ import org .springframework .security .core .context .SecurityContext ;
30
+ import org .springframework .security .core .context .SecurityContextHolder ;
27
31
import org .springframework .security .web .authentication .preauth .PreAuthenticatedAuthenticationToken ;
28
32
import org .springframework .stereotype .Component ;
29
33
import retrofit .RetrofitError ;
30
34
31
35
import java .io .Serializable ;
36
+ import java .util .Set ;
37
+ import java .util .function .Function ;
32
38
33
39
@ Component
34
40
@ Slf4j
@@ -57,7 +63,9 @@ public boolean hasPermission(Authentication authentication, Serializable resourc
57
63
Resource r = Resource .parse (resourceType );
58
64
Authorization a = Authorization .valueOf (authorization .toString ());
59
65
60
- return isAuthorized (username , r , resourceName .toString (), a );
66
+ return isWholePermissionStored (authentication ) ?
67
+ permissionContains (authentication , resourceName .toString (), r , a ) :
68
+ isAuthorized (username , r , resourceName .toString (), a );
61
69
}
62
70
63
71
private String getUsername (Authentication authentication ) {
@@ -83,4 +91,65 @@ private boolean isAuthorized(String username, Resource resource, String resource
83
91
}
84
92
return true ;
85
93
}
94
+
95
+ @ SuppressWarnings ("unused" )
96
+ public boolean storeWholePermission () {
97
+ if (!Boolean .valueOf (fiatEnabled )) {
98
+ return true ;
99
+ }
100
+
101
+ String username = getUsername (SecurityContextHolder .getContext ().getAuthentication ());
102
+
103
+ UserPermission .View view ;
104
+ try {
105
+ view = fiatService .getUserPermission (username );
106
+ } catch (RetrofitError re ) {
107
+ String message = String .format ("Cannot get whole user permission for user %s" , username );
108
+ log .debug (message );
109
+ log .trace (message , re );
110
+ return false ;
111
+ }
112
+
113
+ PreAuthenticatedAuthenticationToken auth = new PreAuthenticatedAuthenticationToken (username , null , null );
114
+ auth .setDetails (view );
115
+
116
+ SecurityContext ctx = SecurityContextHolder .createEmptyContext ();
117
+ ctx .setAuthentication (auth );
118
+ SecurityContextHolder .setContext (ctx );
119
+
120
+ return true ;
121
+ }
122
+
123
+ private boolean isWholePermissionStored (Authentication authentication ) {
124
+ return authentication .getDetails () != null &&
125
+ authentication .getDetails () instanceof UserPermission .View ;
126
+ }
127
+
128
+ private boolean permissionContains (Authentication authentication ,
129
+ String resourceName ,
130
+ Resource resource ,
131
+ Authorization authorization ) {
132
+ UserPermission .View permission = (UserPermission .View ) authentication .getDetails ();
133
+
134
+ Function <Set <? extends Authorizable >, Boolean > containsAuth = resources ->
135
+ resources
136
+ .stream ()
137
+ .anyMatch (view -> view .getName ().equalsIgnoreCase (resourceName ) &&
138
+ view .getAuthorizations ().contains (authorization ));
139
+
140
+
141
+ switch (resource ) {
142
+ case ACCOUNT :
143
+ return containsAuth .apply (permission .getAccounts ());
144
+ case APPLICATION :
145
+ return containsAuth .apply (permission .getApplications ());
146
+ default :
147
+ return false ;
148
+ }
149
+ }
150
+
151
+ @ SuppressWarnings ("unused" )
152
+ public boolean isAdmin () {
153
+ return true ; // TODO(ttomsu): Chosen by fair dice roll. Guaranteed to be random.
154
+ }
86
155
}
0 commit comments