/
SAREAuthorization.java
274 lines (234 loc) · 10.6 KB
/
SAREAuthorization.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright 2010 DTO Labs, Inc. (http://dtolabs.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
package com.dtolabs.rundeck.core.authorization.providers;
import com.dtolabs.rundeck.core.authorization.Attribute;
import com.dtolabs.rundeck.core.authorization.Authorization;
import com.dtolabs.rundeck.core.authorization.Decision;
import com.dtolabs.rundeck.core.authorization.Explanation;
import com.dtolabs.rundeck.core.authorization.Explanation.Code;
import com.dtolabs.rundeck.core.authorization.providers.Policies.Context;
import com.dtolabs.rundeck.core.authorization.providers.Policies.ContextDecision;
import org.apache.log4j.Logger;
import javax.security.auth.Subject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.security.Principal;
import java.util.*;
/**
* Given a Subject, Action, Resource and Environment deliver an authorization decision.
*
* @author noahcampbell
*
*/
public class SAREAuthorization implements Authorization {
private final static Logger logger = Logger.getLogger(SAREAuthorization.class);
private final Policies policies;
private final File baseDirectory;
private long decisionsMade;
/**
* Create an authorization object that uses understands the .aclpolicy files.
*
* @param directory The directory to ready *.aclpolicy from.
*
* @throws IOException
* @throws PoliciesParseException
*/
public SAREAuthorization(File directory) throws IOException, PoliciesParseException {
policies = Policies.load(directory);
baseDirectory = directory;
}
/**
* Convenience constructor that looks in a predefine spot for policy files.
*
* @throws IOException
* @throws PoliciesParseException
*/
public SAREAuthorization() throws IOException, PoliciesParseException {
this(new File("/etc/rundeck/security.d/"));
}
/**
*
* @param resource
* @param subject
* @param action
* @param environment
* @return decision
*/
private Decision internalEvaluate(Map<String, String> resource, Subject subject, String action,
Set<Attribute> environment) {
long start = System.currentTimeMillis();
if(resource == null) {
throw new IllegalArgumentException("Resource does not identify any resource because it's an empty resource property or null.");
} else {
for(Map.Entry<String, String> entry : resource.entrySet()) {
if(entry.getKey() == null) {
throw new IllegalArgumentException("Resource definition cannot contain null property name.");
}
if(entry.getValue() == null) {
throw new IllegalArgumentException("Resource definition cannot contain null value. Corresponding key: " + entry.getKey());
}
}
}
if(subject == null) throw new IllegalArgumentException("Invalid subject, subject is null.");
if(action == null || action.length() <= 0) {
return authorize(false, "No action provided.", Code.REJECTED_NO_ACTION_PROVIDED, resource, subject, action, environment, System.currentTimeMillis() - start);
}
// environment can be null.
if(environment == null) {
environment = Collections.emptySet();
}
this.decisionsMade++;
long narrowStart = System.currentTimeMillis();
List<Context> contexts = policies.narrowContext(subject, environment);
long narrowDuration = System.currentTimeMillis() - narrowStart;
if(contexts.size() <= 0) {
return authorize(false, "No context matches subject or environment", Code.REJECTED_NO_SUBJECT_OR_ENV_FOUND, resource, subject, action, environment, System.currentTimeMillis() - start);
}
ContextDecision contextDecision = null;
long contextIncludeStart = System.currentTimeMillis();
for(Context ctx : contexts) {
contextDecision = ctx.includes(resource, action);
if(contextDecision.granted()) {
return createAuthorize(true, contextDecision, resource, subject, action, environment, System.currentTimeMillis() - start);
}
}
if(contextDecision == null) {
return authorize(false, "No resource or action matched.",
Code.REJECTED_NO_RESOURCE_OR_ACTION_MATCH, resource, subject, action, environment, System.currentTimeMillis() - start);
} else {
return createAuthorize(false, contextDecision, resource, subject, action, environment, System.currentTimeMillis() - start);
}
}
/**
*
*/
public Decision evaluate(Map<String, String> resource, Subject subject,
String action, Set<Attribute> environment) {
Decision decision = internalEvaluate(resource, subject, action, environment);
StringBuilder sb = new StringBuilder();
sb.append("Evaluating ").append(decision).append(" (").append(decision.evaluationDuration()).append("ms)").append(':');
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream pw = new PrintStream(out, true);
decision.explain().describe(pw);
sb.append(out.toString());
System.err.println(sb.toString());
logger.info(sb.toString());
return decision;
}
public Set<Decision> evaluate(Set<Map<String, String>> resources, Subject subject, Set<String> actions,
Set<Attribute> environment) {
Set<Decision> decisions = new HashSet<Decision>();
long duration=0;
for(Map<String, String> resource: resources) {
for(String action: actions) {
final Decision decision = evaluate(resource, subject, action, environment);
duration += decision.evaluationDuration();
decisions.add(decision);
}
}
return decisions;
}
private static Decision authorize(final boolean authorized, final String reason,
final Code reasonId, final Map<String, String> resource, final Subject subject,
final String action, final Set<Attribute> environment, final long evaluationTime) {
return createAuthorize(authorized, new Explanation() {
public Code getCode() {
return reasonId;
}
public void describe(PrintStream out) {
out.println(toString());
}
public String toString() {
return "\t" + reason + " => " + reasonId;
}
}, resource, subject, action, environment, evaluationTime);
}
private static Decision createAuthorize(final boolean authorized, final Explanation explanation,
final Map<String, String> resource, final Subject subject,
final String action, final Set<Attribute> environment, final long evaluationTime) {
return new Decision(){
private String representation;
public boolean isAuthorized() { return authorized; }
public Map<String, String> getResource() { return resource; }
public String getAction() { return action; }
public Set<Attribute> getEnvironment() { return environment; }
public Subject getSubject() { return subject; }
public String toString() {
if(representation == null) {
StringBuilder builder = new StringBuilder();
builder.append("Decision for: ");
builder.append("res<");
Iterator<Map.Entry<String,String>> riter = resource.entrySet().iterator();
while(riter.hasNext()) {
Map.Entry<String, String> s = riter.next();
builder.append(s.getKey()).append(':').append(s.getValue());
if(riter.hasNext()) {
builder.append(", ");
}
}
builder.append("> subject<");
Iterator<Principal> iter = subject.getPrincipals().iterator();
while(iter.hasNext()) {
Principal principal = iter.next();
builder.append(principal.getClass().getSimpleName());
builder.append(':');
builder.append(principal.getName());
if(iter.hasNext()) {
builder.append(' ');
}
}
builder.append("> action<");
builder.append(action);
builder.append("> env<");
Iterator<Attribute> eiter = environment.iterator();
while(eiter.hasNext()) {
Attribute a = eiter.next();
builder.append(a);
if(eiter.hasNext()) {
builder.append(", ");
}
}
builder.append(">");
this.representation = builder.toString();
}
return this.representation;
}
public Explanation explain() {
return explanation;
}
public long evaluationDuration() {
return evaluationTime;
}
};
}
@Override
public String toString() {
return getClass().getName() + " (" + this.policies.count() + ") [" + this.baseDirectory.toString() + "]";
}
/**
* This WILL be refactored.
* @return
*/
@Deprecated
public List<String> hackMeSomeRoles() {
return policies.listAllRoles();
}
}