-
Notifications
You must be signed in to change notification settings - Fork 131
/
PrefixRestriction.java
166 lines (141 loc) · 5.33 KB
/
PrefixRestriction.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
/*
* dCache - http://www.dcache.org/
*
* Copyright (C) 2016 Deutsches Elektronen-Synchrotron
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dcache.auth.attributes;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import diskCacheV111.util.FsPath;
import java.io.IOException;
import java.util.function.Function;
/**
* A Restriction that allows a user to perform only activity on paths with a particular prefix.
*/
public class PrefixRestriction implements Restriction {
private static final long serialVersionUID = 7073397935939729478L;
private ImmutableSet<FsPath> prefixes;
private transient Function<FsPath, FsPath> resolver;
public PrefixRestriction(FsPath... prefixes) {
this.prefixes = ImmutableSet.copyOf(prefixes);
}
public ImmutableSet<FsPath> getPrefixes() {
return prefixes;
}
@Override
public boolean isRestricted(Activity activity, FsPath path) {
return isRestricted(activity, path, false);
}
@Override
public boolean isRestricted(Activity activity, FsPath directory, String child,
boolean skipPrefixCheck) {
return isRestricted(activity, directory.child(child), skipPrefixCheck);
}
@Override
public boolean hasUnrestrictedChild(Activity activity, FsPath parent) {
Function<FsPath, FsPath> resolver = getPathResolver();
FsPath resolvedParent = resolver.apply(parent);
return prefixes.stream().map(resolver::apply)
.anyMatch(p -> p.hasPrefix(resolvedParent) && !p.equals(resolvedParent));
}
@Override
public int hashCode() {
return PrefixRestriction.class.hashCode() ^ prefixes.hashCode();
}
@Override
public boolean equals(Object other) {
return other instanceof PrefixRestriction && ((PrefixRestriction) other).prefixes.equals(
prefixes);
}
@Override
public boolean isSubsumedBy(Restriction other) {
return other instanceof PrefixRestriction && ((PrefixRestriction) other).subsumes(this);
}
@Override
public void setPathResolver(Function<FsPath, FsPath> resolver) {
this.resolver = resolver;
}
@Override
public Function<FsPath, FsPath> getPathResolver() {
return resolver != null ? resolver : Restriction.super.getPathResolver();
}
private boolean subsumes(PrefixRestriction restriction) {
for (FsPath prefix : prefixes) {
if (restriction.isRestricted(prefix)) {
return false;
}
}
return true;
}
private boolean isRestricted(FsPath path) {
for (FsPath prefix : prefixes) {
Function<FsPath, FsPath> resolver = getPathResolver();
prefix = resolver.apply(prefix);
path = resolver.apply(path);
if (path.hasPrefix(prefix)) {
return false;
}
}
return true;
}
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException {
int countPrefixes = stream.readInt();
ImmutableSet.Builder<FsPath> builder = ImmutableSet.builder();
for (int i = 0; i < countPrefixes; i++) {
builder.add(FsPath.create(stream.readObject().toString()));
}
prefixes = builder.build();
}
private void writeObject(java.io.ObjectOutputStream stream)
throws IOException {
stream.writeInt(prefixes.size());
for (FsPath prefix : prefixes) {
stream.writeObject(prefix.toString());
}
}
/**
* @return string representation of prefixes before resolution
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder("PrefixRestrict[");
if (prefixes.size() == 1) {
sb.append("prefix=").append(prefixes.iterator().next().toString());
} else {
sb.append("prefixes={").append(Joiner.on(',').join(prefixes)).append('}');
}
return sb.append(']').toString();
}
private boolean isRestricted(Activity activity, FsPath path, boolean skipPrefixCheck) {
if (skipPrefixCheck) {
return !(activity == Activity.READ_METADATA || activity == Activity.LIST);
} else {
for (FsPath prefix : prefixes) {
prefix = getPathResolver().apply(prefix);
path = getPathResolver().apply(path);
if (path.hasPrefix(prefix)) {
return false;
}
if (prefix.hasPrefix(path) && (activity == Activity.READ_METADATA
|| activity == Activity.LIST)) {
return false;
}
}
}
return true;
}
}