Skip to content

Commit

Permalink
RANGER-4285: Ranger Java Patch for adding uiHint in policy condition …
Browse files Browse the repository at this point in the history
…for upgrade scenario

Signed-off-by: Mehul Parikh <mehul@apache.org>
  • Loading branch information
Sanket-Shelar authored and mehulbparikh committed Aug 30, 2023
1 parent c1dcb78 commit a534a51
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1879,4 +1879,5 @@ INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10054',UTC_TIMESTAMP(),'Ranger 3.0.0',UTC_TIMESTAMP(),'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10055',UTC_TIMESTAMP(),'Ranger 3.0.0',UTC_TIMESTAMP(),'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10056',UTC_TIMESTAMP(),'Ranger 3.0.0',UTC_TIMESTAMP(),'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10057',UTC_TIMESTAMP(),'Ranger 3.0.0',UTC_TIMESTAMP(),'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('JAVA_PATCHES',UTC_TIMESTAMP(),'Ranger 1.0.0',UTC_TIMESTAMP(),'localhost','Y');
Original file line number Diff line number Diff line change
Expand Up @@ -2068,5 +2068,6 @@ INSERT INTO x_db_version_h (id,version,inst_at,inst_by,updated_at,updated_by,act
INSERT INTO x_db_version_h (id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES (X_DB_VERSION_H_SEQ.nextval,'J10054',sys_extract_utc(systimestamp),'Ranger 3.0.0',sys_extract_utc(systimestamp),'localhost','Y');
INSERT INTO x_db_version_h (id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES (X_DB_VERSION_H_SEQ.nextval,'J10055',sys_extract_utc(systimestamp),'Ranger 3.0.0',sys_extract_utc(systimestamp),'localhost','Y');
INSERT INTO x_db_version_h (id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES (X_DB_VERSION_H_SEQ.nextval,'J10056',sys_extract_utc(systimestamp),'Ranger 3.0.0',sys_extract_utc(systimestamp),'localhost','Y');
INSERT INTO x_db_version_h (id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES (X_DB_VERSION_H_SEQ.nextval,'J10057',sys_extract_utc(systimestamp),'Ranger 3.0.0',sys_extract_utc(systimestamp),'localhost','Y');
INSERT INTO x_db_version_h (id,version,inst_at,inst_by,updated_at,updated_by,active) VALUES (X_DB_VERSION_H_SEQ.nextval,'JAVA_PATCHES',sys_extract_utc(systimestamp),'Ranger 1.0.0',sys_extract_utc(systimestamp),'localhost','Y');
commit;
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,7 @@ INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10054',current_timestamp,'Ranger 3.0.0',current_timestamp,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10055',current_timestamp,'Ranger 3.0.0',current_timestamp,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10056',current_timestamp,'Ranger 3.0.0',current_timestamp,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10057',current_timestamp,'Ranger 3.0.0',current_timestamp,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('JAVA_PATCHES',current_timestamp,'Ranger 1.0.0',current_timestamp,'localhost','Y');

DROP VIEW IF EXISTS vx_trx_log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,8 @@ INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active
GO
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10056',CURRENT_TIMESTAMP,'Ranger 3.0.0',CURRENT_TIMESTAMP,'localhost','Y');
GO
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10057',CURRENT_TIMESTAMP,'Ranger 3.0.0',CURRENT_TIMESTAMP,'localhost','Y');
GO
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('JAVA_PATCHES',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
GO
exit
Original file line number Diff line number Diff line change
Expand Up @@ -4236,6 +4236,7 @@ INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10054',CURRENT_TIMESTAMP,'Ranger 3.0.0',CURRENT_TIMESTAMP,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10055',CURRENT_TIMESTAMP,'Ranger 3.0.0',CURRENT_TIMESTAMP,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10056',CURRENT_TIMESTAMP,'Ranger 3.0.0',CURRENT_TIMESTAMP,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('J10057',CURRENT_TIMESTAMP,'Ranger 3.0.0',CURRENT_TIMESTAMP,'localhost','Y');
INSERT INTO x_db_version_h (version,inst_at,inst_by,updated_at,updated_by,active) VALUES ('JAVA_PATCHES',CURRENT_TIMESTAMP,'Ranger 1.0.0',CURRENT_TIMESTAMP,'localhost','Y');
GO
CREATE VIEW [dbo].[vx_trx_log] AS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.ranger.patch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ranger.biz.ServiceDBStore;
import org.apache.ranger.plugin.model.RangerServiceDef;
import org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef;
import org.apache.ranger.plugin.store.EmbeddedServiceDefsUtil;
import org.apache.ranger.plugin.util.SearchFilter;
import org.apache.ranger.util.CLIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PatchForAllServiceDefForPolicyConditionUpdate_J10057 extends BaseLoader{
private static final Logger logger = LoggerFactory.getLogger(PatchForAllServiceDefForPolicyConditionUpdate_J10057.class);

@Autowired
ServiceDBStore svcStore;

public static void main(String[] args) {
logger.info("main()");
try {
PatchForAllServiceDefForPolicyConditionUpdate_J10057 loader = (PatchForAllServiceDefForPolicyConditionUpdate_J10057) CLIUtil.getBean(PatchForAllServiceDefForPolicyConditionUpdate_J10057.class);
loader.init();
while (loader.isMoreToProcess()) {
loader.load();
}
logger.info("Load complete. Exiting!!!");
System.exit(0);
} catch (Exception e) {
logger.error("Error loading", e);
System.exit(1);
}
}

@Override
public void init() throws Exception {
// Do Nothing
}

@Override
public void execLoad() {
logger.info("==> PatchForAllServiceDefForPolicyConditionUpdate_J10057.execLoad()");
try {
updateAllServiceDef();
} catch (Exception e) {
logger.error("Error whille PatchForAllServiceDefForPolicyConditionUpdate_J10057()data.", e);
}
logger.info("<== PatchForAllServiceDefForPolicyConditionUpdate_J10057.execLoad()");
}

@Override
public void printStats() {
logger.info("PatchForAllServiceDefForPolicyConditionUpdate_J10057 data ");
}

private void updateAllServiceDef() {

try {
List<RangerServiceDef> allServiceDefs = svcStore.getServiceDefs(new SearchFilter());

if (CollectionUtils.isNotEmpty(allServiceDefs)) {
for (RangerServiceDef serviceDef : allServiceDefs) {
if(CollectionUtils.isNotEmpty(serviceDef.getPolicyConditions())) {
Map<Long,String> uiHintPreVal = new HashMap<>();
List<RangerPolicyConditionDef> updatedPolicyCondition = new ArrayList<>();
RangerServiceDef embeddedTagServiceDef=EmbeddedServiceDefsUtil.instance().getEmbeddedServiceDef(serviceDef.getName());

List<RangerPolicyConditionDef> policyConditionsOld = embeddedTagServiceDef.getPolicyConditions();
for(RangerPolicyConditionDef policyConditionOld : policyConditionsOld) {
uiHintPreVal.put(policyConditionOld.getItemId(), policyConditionOld.getUiHint());
}

List<RangerPolicyConditionDef> policyConditionsNew = serviceDef.getPolicyConditions();
for (RangerPolicyConditionDef policyConditionNew : policyConditionsNew) {
if(StringUtils.isNotEmpty(uiHintPreVal.get(policyConditionNew.getItemId()))) {
policyConditionNew.setUiHint(uiHintPreVal.get(policyConditionNew.getItemId()));
}
updatedPolicyCondition.add(policyConditionNew);
}

serviceDef.setPolicyConditions(updatedPolicyCondition);
svcStore.updateServiceDef(serviceDef);
}
}
}
}catch (Exception e) {
logger.error("Error while patching service-def for policy condition:", e);
}
}
}

0 comments on commit a534a51

Please sign in to comment.