Skip to content

Commit 3db1860

Browse files
committed
Merge remote-tracking branch 'origin/RANGER-5309' into RANGER-5312
2 parents 7974e5e + 54d1bb3 commit 3db1860

2 files changed

Lines changed: 362 additions & 0 deletions

File tree

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.ranger.admin.client;
21+
22+
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.ranger.plugin.util.JsonUtilsV2;
24+
import org.apache.ranger.plugin.util.RangerRoles;
25+
import org.apache.ranger.plugin.util.RangerUserStore;
26+
import org.apache.ranger.plugin.util.ServiceGdsInfo;
27+
import org.apache.ranger.plugin.util.ServicePolicies;
28+
import org.apache.ranger.plugin.util.ServiceTags;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
import java.io.InputStream;
33+
import java.io.InputStreamReader;
34+
35+
// this implementation loads policies, roles, tags, userstore and gds info from embedded resources at following paths:
36+
// {resource-path}/{appId}_{serviceName}.json -> policies
37+
// {resource-path}/{appId}_{serviceName}_roles.json -> roles
38+
// {resource-path}/{appId}_{serviceName}_tag.json -> tags
39+
// {resource-path}/{appId}_{serviceName}_userstore.json -> userstore
40+
// {resource-path}/{appId}_{serviceName}_gds.json -> gds info
41+
public class EmbeddedResourcePolicySource extends AbstractRangerAdminClient {
42+
private static final Logger LOG = LoggerFactory.getLogger(EmbeddedResourcePolicySource.class);
43+
44+
private ServicePolicies policies;
45+
private RangerRoles roles;
46+
private ServiceTags tags;
47+
private RangerUserStore userStore;
48+
private ServiceGdsInfo gdsInfo;
49+
50+
private String policiesPath;
51+
private String rolesPath;
52+
private String tagsPath;
53+
private String userStorePath;
54+
private String gdsInfoPath;
55+
56+
@Override
57+
public void init(String serviceName, String appId, String configPropertyPrefix, Configuration config) {
58+
super.init(serviceName, appId, configPropertyPrefix, config);
59+
60+
String directory = config.get(configPropertyPrefix + ".policy.source.embedded_resource.path");
61+
String pathPrefix = (directory == null ? "" : directory) + "/" + appId + "_" + serviceName;
62+
63+
if (!pathPrefix.startsWith("/")) {
64+
pathPrefix = "/" + pathPrefix;
65+
}
66+
67+
this.policiesPath = pathPrefix + ".json";
68+
this.rolesPath = pathPrefix + "_roles.json";
69+
this.tagsPath = pathPrefix + "_tag.json";
70+
this.userStorePath = pathPrefix + "_userstore.json";
71+
this.gdsInfoPath = pathPrefix + "_gds.json";
72+
}
73+
74+
@Override
75+
public ServicePolicies getServicePoliciesIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) {
76+
loadPolicies();
77+
78+
return (lastKnownVersion == -1 || policies == null || policies.getPolicyVersion() == null || !policies.getPolicyVersion().equals(lastKnownVersion)) ? policies : null;
79+
}
80+
81+
@Override
82+
public RangerRoles getRolesIfUpdated(long lastKnownVersion, long lastActivationTimeInMills) {
83+
loadRoles();
84+
85+
return (lastKnownVersion == -1 || roles == null || roles.getRoleVersion() == null || !roles.getRoleVersion().equals(lastKnownVersion)) ? roles : null; }
86+
87+
@Override
88+
public ServiceTags getServiceTagsIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) {
89+
loadTags();
90+
91+
return (lastKnownVersion == -1 || tags == null || tags.getTagVersion() == null || !tags.getTagVersion().equals(lastKnownVersion)) ? tags : null;
92+
}
93+
94+
@Override
95+
public RangerUserStore getUserStoreIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) {
96+
loadUserStore();
97+
98+
return (lastKnownVersion == -1 || userStore == null || userStore.getUserStoreVersion() == null || !userStore.getUserStoreVersion().equals(lastKnownVersion)) ? userStore : null;
99+
}
100+
101+
@Override
102+
public ServiceGdsInfo getGdsInfoIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) {
103+
loadGdsInfo();
104+
105+
return (lastKnownVersion == -1 || gdsInfo == null || gdsInfo.getGdsVersion() == null || !gdsInfo.getGdsVersion().equals(lastKnownVersion)) ? gdsInfo : null;
106+
}
107+
108+
private void loadPolicies() {
109+
if (policies == null) {
110+
try {
111+
InputStream input = getClass().getResourceAsStream(policiesPath);
112+
113+
if (input != null) {
114+
policies = gson.fromJson(new InputStreamReader(input), ServicePolicies.class);
115+
}
116+
} catch (Throwable t) {
117+
LOG.error("loadPolicies(): failed to load policies from {}", policiesPath, t);
118+
}
119+
}
120+
}
121+
122+
private void loadRoles() {
123+
if (roles == null) {
124+
try {
125+
InputStream input = getClass().getResourceAsStream(rolesPath);
126+
127+
if (input != null) {
128+
roles = gson.fromJson(new InputStreamReader(input), RangerRoles.class);
129+
}
130+
} catch (Throwable t) {
131+
LOG.error("loadRoles(): failed to load roles from {}", rolesPath, t);
132+
}
133+
}
134+
}
135+
136+
private void loadUserStore() {
137+
if (userStore == null) {
138+
try {
139+
InputStream input = getClass().getResourceAsStream(userStorePath);
140+
141+
if (input != null) {
142+
userStore = gson.fromJson(new InputStreamReader(input), RangerUserStore.class);
143+
}
144+
} catch (Throwable t) {
145+
LOG.error("loadUserStore(): failed to load userstore from {}", userStorePath, t);
146+
}
147+
}
148+
}
149+
150+
private void loadTags() {
151+
if (tags == null) {
152+
try {
153+
InputStream input = getClass().getResourceAsStream(tagsPath);
154+
155+
if (input != null) {
156+
tags = gson.fromJson(new InputStreamReader(input), ServiceTags.class);
157+
}
158+
} catch (Throwable t) {
159+
LOG.error("loadTags(): failed to load tags from {}", tagsPath, t);
160+
}
161+
}
162+
}
163+
164+
private void loadGdsInfo() {
165+
if (gdsInfo == null) {
166+
try {
167+
InputStream input = getClass().getResourceAsStream(gdsInfoPath);
168+
169+
if (input != null) {
170+
gdsInfo = JsonUtilsV2.readValue(new InputStreamReader(input), ServiceGdsInfo.class);
171+
}
172+
} catch (Throwable t) {
173+
LOG.error("loadGdsInfo(): failed to load gdsInfo from {}", gdsInfoPath, t);
174+
}
175+
}
176+
}
177+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.ranger.admin.client;
21+
22+
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.ranger.plugin.util.JsonUtilsV2;
24+
import org.apache.ranger.plugin.util.RangerRoles;
25+
import org.apache.ranger.plugin.util.RangerUserStore;
26+
import org.apache.ranger.plugin.util.ServiceGdsInfo;
27+
import org.apache.ranger.plugin.util.ServicePolicies;
28+
import org.apache.ranger.plugin.util.ServiceTags;
29+
30+
import java.io.File;
31+
import java.io.FileReader;
32+
33+
// this implementation loads policies, roles, tags, userstore and gds info from the given local filesystem paths:
34+
// {path}/{appId}_{serviceName}.json -> policies
35+
// {path}/{appId}_{serviceName}_roles.json -> roles
36+
// {path}/{appId}_{serviceName}_tag.json -> tags
37+
// {path}/{appId}_{serviceName}_userstore.json -> userstore
38+
// {path}/{appId}_{serviceName}_gds.json -> gds info
39+
public class LocalFolderPolicySource extends AbstractRangerAdminClient {
40+
private ServicePolicies policies;
41+
private RangerRoles roles;
42+
private RangerUserStore userStore;
43+
private ServiceTags tags;
44+
private ServiceGdsInfo gdsInfo;
45+
46+
private String policiesPath;
47+
private String rolesPath;
48+
private String userStorePath;
49+
private String tagsPath;
50+
private String gdsInfoPath;
51+
private long lastPoliciesFileModifiedTime = -1;
52+
private long lastRolesFileModifiedTime = -1;
53+
private long lastUserStoreFileModifiedTime = -1;
54+
private long lastTagsFileModifiedTime = -1;
55+
private long lastGdsInfoFileModifiedTime = -1;
56+
57+
@Override
58+
public void init(String serviceName, String appId, String configPropertyPrefix, Configuration config) {
59+
super.init(serviceName, appId, configPropertyPrefix, config);
60+
61+
String directory = config.get(configPropertyPrefix + ".policy.source.local_folder.path");
62+
String pathPrefix = (directory == null ? "" : directory) + File.separator + appId + "_" + serviceName;
63+
64+
this.policiesPath = pathPrefix + ".json";
65+
this.rolesPath = pathPrefix + "_roles.json";
66+
this.userStorePath = pathPrefix + "_userstore.json";
67+
this.tagsPath = pathPrefix + "_tag.json";
68+
this.gdsInfoPath = pathPrefix + "_gds.json";
69+
}
70+
71+
@Override
72+
public ServicePolicies getServicePoliciesIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) throws Exception {
73+
loadPolicies();
74+
75+
return (lastKnownVersion == -1 || policies == null || policies.getPolicyVersion() == null || !policies.getPolicyVersion().equals(lastKnownVersion)) ? policies : null;
76+
}
77+
78+
@Override
79+
public RangerRoles getRolesIfUpdated(long lastKnownVersion, long lastActivationTimeInMills) throws Exception {
80+
loadRoles();
81+
82+
return (lastKnownVersion == -1 || roles == null || roles.getRoleVersion() == null || !roles.getRoleVersion().equals(lastKnownVersion)) ? roles : null;
83+
}
84+
85+
@Override
86+
public ServiceTags getServiceTagsIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) throws Exception {
87+
loadTags();
88+
89+
return (lastKnownVersion == -1 || tags == null || tags.getTagVersion() == null || !tags.getTagVersion().equals(lastKnownVersion)) ? tags : null;
90+
}
91+
92+
@Override
93+
public RangerUserStore getUserStoreIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) throws Exception {
94+
loadUserStore();
95+
96+
return (lastKnownVersion == -1 || userStore == null || userStore.getUserStoreVersion() == null || !userStore.getUserStoreVersion().equals(lastKnownVersion)) ? userStore : null;
97+
}
98+
99+
@Override
100+
public ServiceGdsInfo getGdsInfoIfUpdated(long lastKnownVersion, long lastActivationTimeInMillis) throws Exception {
101+
loadGdsInfo();
102+
103+
return (lastKnownVersion == -1 || gdsInfo == null || gdsInfo.getGdsVersion() == null || !gdsInfo.getGdsVersion().equals(lastKnownVersion)) ? gdsInfo : null;
104+
}
105+
106+
private void loadPolicies() throws Exception {
107+
File srcFile = new File(policiesPath);
108+
109+
if (!srcFile.exists() || !srcFile.canRead()) {
110+
throw new Exception(policiesPath + ": policies file not found or not readable");
111+
}
112+
113+
if (policies == null || srcFile.lastModified() != lastPoliciesFileModifiedTime) {
114+
try (FileReader reader = new FileReader(srcFile)) {
115+
policies = gson.fromJson(reader, ServicePolicies.class);
116+
117+
lastPoliciesFileModifiedTime = srcFile.lastModified();
118+
}
119+
}
120+
}
121+
122+
private void loadRoles() throws Exception {
123+
File srcFile = new File(rolesPath);
124+
125+
if (!srcFile.exists() || !srcFile.canRead()) {
126+
throw new Exception(rolesPath + ": roles file not found or not readable");
127+
}
128+
129+
if (roles == null || srcFile.lastModified() != lastRolesFileModifiedTime) {
130+
try (FileReader reader = new FileReader(srcFile)) {
131+
roles = gson.fromJson(reader, RangerRoles.class);
132+
133+
lastRolesFileModifiedTime = srcFile.lastModified();
134+
}
135+
}
136+
}
137+
138+
private void loadTags() throws Exception {
139+
File srcFile = new File(tagsPath);
140+
141+
if (!srcFile.exists() || !srcFile.canRead()) {
142+
throw new Exception(tagsPath + ": tags file not found or not readable");
143+
}
144+
145+
if (tags == null || srcFile.lastModified() != lastTagsFileModifiedTime) {
146+
try (FileReader reader = new FileReader(srcFile)) {
147+
tags = gson.fromJson(reader, ServiceTags.class);
148+
149+
lastTagsFileModifiedTime = srcFile.lastModified();
150+
}
151+
}
152+
}
153+
154+
private void loadUserStore() throws Exception {
155+
File srcFile = new File(userStorePath);
156+
157+
if (!srcFile.exists() || !srcFile.canRead()) {
158+
throw new Exception(userStorePath + ": userStore file not found or not readable");
159+
}
160+
161+
if (userStore == null || srcFile.lastModified() != lastUserStoreFileModifiedTime) {
162+
try (FileReader reader = new FileReader(srcFile)) {
163+
userStore = gson.fromJson(reader, RangerUserStore.class);
164+
165+
lastUserStoreFileModifiedTime = srcFile.lastModified();
166+
}
167+
}
168+
}
169+
170+
private void loadGdsInfo() throws Exception {
171+
File srcFile = new File(gdsInfoPath);
172+
173+
if (!srcFile.exists() || !srcFile.canRead()) {
174+
throw new Exception(gdsInfoPath + ": gdsInfo file not found or not readable");
175+
}
176+
177+
if (gdsInfo == null || srcFile.lastModified() != lastGdsInfoFileModifiedTime) {
178+
try (FileReader reader = new FileReader(srcFile)) {
179+
gdsInfo = JsonUtilsV2.readValue(reader, ServiceGdsInfo.class);
180+
181+
lastGdsInfoFileModifiedTime = srcFile.lastModified();
182+
}
183+
}
184+
}
185+
}

0 commit comments

Comments
 (0)