|
| 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