forked from wildfly/wildfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertiesRealm.java
128 lines (109 loc) · 4.53 KB
/
PropertiesRealm.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
/*
* Copyright 2016 Red Hat, Inc.
*
* 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 org.wildfly.test.security.common.elytron;
import static org.jboss.as.test.integration.security.common.Utils.createTemporaryFolder;
import static org.jboss.as.test.shared.CliUtils.asAbsolutePath;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.jboss.as.test.integration.management.util.CLIWrapper;
import org.jboss.logging.Logger;
/**
* Configuration for properties-realms Elytron resource.
*
* @author Josef Cacek
*/
public class PropertiesRealm extends AbstractUserAttributeValuesCapableElement implements SecurityRealm {
private static final Logger LOGGER = Logger.getLogger(PropertiesRealm.class);
private final String groupsAttribute;
private final boolean plainText; // true by default
private File tempFolder;
private PropertiesRealm(Builder builder) {
super(builder);
this.groupsAttribute = builder.groupsAttribute;
this.plainText = builder.plainText;
}
@Override
public void create(CLIWrapper cli) throws Exception {
this.tempFolder = createTemporaryFolder("ely-" + name);
final Properties usersProperties = new Properties();
final Properties rolesProperties = new Properties();
for (UserWithAttributeValues user : getUsersWithAttributeValues()) {
usersProperties.setProperty(user.getName(), user.getPassword());
rolesProperties.setProperty(user.getName(), String.join(",", user.getValues()));
}
File usersFile = writeProperties(usersProperties, "users.properties");
File rolesFile = writeProperties(rolesProperties, "roles.properties");
// /subsystem=elytron/properties-realm=test:add(users-properties={path=/tmp/users.properties, plain-text=true},
// groups-properties={path=/tmp/groups.properties}, groups-attribute="groups")
final String groupsAttrStr = groupsAttribute == null ? "" : String.format(", groups-attribute=\"%s\"", groupsAttribute);
cli.sendLine(String.format(
"/subsystem=elytron/properties-realm=%s:add(users-properties={path=\"%s\", plain-text=%b}, groups-properties={path=\"%s\"}%s)",
name, asAbsolutePath(usersFile), plainText, asAbsolutePath(rolesFile), groupsAttrStr));
}
@Override
public void remove(CLIWrapper cli) throws Exception {
cli.sendLine(String.format("/subsystem=elytron/properties-realm=%s:remove()", name));
FileUtils.deleteQuietly(tempFolder);
tempFolder = null;
}
/**
* Creates builder to build {@link PropertiesRealm}.
*
* @return created builder
*/
public static Builder builder() {
return new Builder();
}
private File writeProperties(Properties properties, String fileName) throws IOException {
File result = new File(tempFolder, fileName);
LOGGER.debugv("Creating property file {0}", result);
try (FileOutputStream fos = new FileOutputStream(result)) {
if (plainText) {
properties.store(fos, null);
} else {
properties.store(fos, "$REALM_NAME=" + name + "$");
}
}
return result;
}
/**
* Builder to build {@link PropertiesRealm}.
*/
public static final class Builder extends AbstractUserAttributeValuesCapableElement.Builder<Builder> {
private String groupsAttribute;
private boolean plainText = true;
private Builder() {
}
public Builder withGroupsAttribute(String groupsAttribute) {
this.groupsAttribute = groupsAttribute;
return this;
}
public Builder withPlainText(boolean plainText) {
this.plainText = plainText;
return this;
}
public PropertiesRealm build() {
return new PropertiesRealm(this);
}
@Override
public Builder self() {
return this;
}
}
}