Skip to content

Commit f424bd9

Browse files
srinathgitaebadirad
authored andcommitted
Create privileges only if they aren't present (#1481)
* Create privileges only if they aren't present * Added gradle tasks for creating privileges * Read the privilege files from the InputStream
1 parent 5e83acf commit f424bd9

File tree

6 files changed

+142
-10
lines changed

6 files changed

+142
-10
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2012-2018 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.hub.deploy.commands;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.apache.commons.io.IOUtils;
24+
import org.springframework.core.io.ClassPathResource;
25+
26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import com.fasterxml.jackson.databind.node.ObjectNode;
28+
import com.marklogic.appdeployer.command.CommandContext;
29+
import com.marklogic.appdeployer.command.security.DeployPrivilegesCommand;
30+
import com.marklogic.hub.error.DataHubConfigurationException;
31+
import com.marklogic.mgmt.ManageClient;
32+
import com.marklogic.mgmt.resource.ResourceManager;
33+
34+
public class DeployHubPrivilegesCommand extends DeployPrivilegesCommand {
35+
private List<String> payLoads;
36+
37+
public DeployHubPrivilegesCommand() {
38+
super();
39+
payLoads = new ArrayList<String>();
40+
try (
41+
InputStream is1 = new ClassPathResource("hub-internal-config/security/privileges/dhf-internal-data-hub.json").getInputStream();
42+
InputStream is2 = new ClassPathResource("hub-internal-config/security/privileges/dhf-internal-entities.json").getInputStream();
43+
InputStream is3 = new ClassPathResource("hub-internal-config/security/privileges/dhf-internal-mappings.json").getInputStream();
44+
InputStream is4 = new ClassPathResource("hub-internal-config/security/privileges/dhf-internal-trace-ui.json").getInputStream();
45+
) {
46+
this.payLoads.add(IOUtils.toString(is1, "utf-8"));
47+
this.payLoads.add(IOUtils.toString(is2, "utf-8"));
48+
this.payLoads.add(IOUtils.toString(is3, "utf-8"));
49+
this.payLoads.add(IOUtils.toString(is4, "utf-8"));
50+
} catch (IOException e) {
51+
throw new DataHubConfigurationException(e);
52+
}
53+
}
54+
55+
/**
56+
* Deploys the privileges if they are not already present
57+
* @param context The command context for execution.
58+
*/
59+
@Override
60+
public void execute(CommandContext context) {
61+
payLoads.stream().forEach((String payLoad)->{
62+
ObjectNode node = null;
63+
try {
64+
node = new ObjectMapper().readValue(payLoad, ObjectNode.class);
65+
} catch (IOException e1) {
66+
throw new DataHubConfigurationException(e1);
67+
}
68+
String privName = node.get("privilege-name").asText();
69+
ManageClient manageClient = context.getManageClient();
70+
try {
71+
manageClient.getJsonAsSecurityUser("/manage/v2/privileges/"+ privName + "/properties?kind=uri");
72+
}
73+
catch(Exception e) {
74+
logger.info("Creating privilege "+privName);
75+
manageClient.postJsonAsSecurityUser("/manage/v2/privileges/", payLoad);
76+
}
77+
});
78+
}
79+
80+
/**
81+
* Undeploys the privileges
82+
* @param context The command context for execution.
83+
*/
84+
@Override
85+
public void undo(CommandContext context) {
86+
logger.info("Removing privileges");
87+
ResourceManager mgr = getResourceManager(context);
88+
for (String f : this.payLoads) {
89+
mgr.delete(f);
90+
91+
}
92+
}
93+
}

marklogic-data-hub/src/main/java/com/marklogic/hub/impl/DataHubImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ private Map<String, List<Command>> getStagingCommands() {
622622
// staging deploys amps.
623623
List<Command> securityCommand = new ArrayList<>();
624624
securityCommand.add(new DeployHubAmpsCommand(hubConfig));
625+
securityCommand.add(new DeployHubPrivilegesCommand());
625626
commandMap.put("mlSecurityCommand", securityCommand);
626627

627628
// don't deploy rest api servers

marklogic-data-hub/src/main/java/com/marklogic/hub/impl/HubProjectImpl.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,20 @@ public HubProjectImpl(String projectDirStr) {
189189
Path userSecurityDir = getUserSecurityDir();
190190
Path rolesDir = hubSecurityDir.resolve("roles");
191191
Path usersDir = hubSecurityDir.resolve("users");
192-
Path privDir = hubSecurityDir.resolve("privileges");
193-
192+
194193
Path userRolesDir = userSecurityDir.resolve("roles");
195194
Path userUsersDir = userSecurityDir.resolve("users");
196195

197196
rolesDir.toFile().mkdirs();
198197
usersDir.toFile().mkdirs();
199-
privDir.toFile().mkdirs();
198+
200199
userRolesDir.toFile().mkdirs();
201200
userUsersDir.toFile().mkdirs();
202201

203202
writeResourceFile("hub-internal-config/security/roles/data-hub-role.json", rolesDir.resolve("data-hub-role.json"), true);
204203
writeResourceFile("hub-internal-config/security/users/data-hub-user.json", usersDir.resolve("data-hub-user.json"), true);
205204
writeResourceFile("hub-internal-config/security/roles/hub-admin-role.json", rolesDir.resolve("hub-admin-role.json"), true);
206-
writeResourceFile("hub-internal-config/security/users/hub-admin-user.json", usersDir.resolve("hub-admin-user.json"), true);
207-
208-
writeResourceFile("hub-internal-config/security/privileges/dhf-internal-data-hub.json", privDir.resolve("dhf-internal-data-hub.json"), true);
209-
writeResourceFile("hub-internal-config/security/privileges/dhf-internal-entities.json", privDir.resolve("dhf-internal-entities.json"), true);
210-
writeResourceFile("hub-internal-config/security/privileges/dhf-internal-mappings.json", privDir.resolve("dhf-internal-mappings.json"), true);
211-
writeResourceFile("hub-internal-config/security/privileges/dhf-internal-trace-ui.json", privDir.resolve("dhf-internal-trace-ui.json"), true);
205+
writeResourceFile("hub-internal-config/security/users/hub-admin-user.json", usersDir.resolve("hub-admin-user.json"), true);
212206

213207
getUserServersDir().toFile().mkdirs();
214208
getUserDatabaseDir().toFile().mkdirs();

ml-data-hub-plugin/src/main/groovy/com/marklogic/gradle/DataHubPlugin.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ class DataHubPlugin implements Plugin<Project> {
9595
project.tasks.replace("mlDeployRoles", DeployHubRolesTask);
9696
project.tasks.replace("mlDeployUsers", DeployHubUsersTask);
9797
project.tasks.replace("mlDeployAmps", DeployHubAmpsTask);
98+
project.tasks.replace("mlDeployPrivileges", DeployHubPrivilegesTask);
9899
project.tasks.replace("mlUndeployRoles", UndeployHubRolesTask);
99100
project.tasks.replace("mlUndeployUsers", UndeployHubUsersTask);
100101
project.tasks.replace("mlUndeployAmps", UndeployHubAmpsTask);
102+
project.tasks.replace("mlUndeployPrivileges", UndeployHubPrivilegesTask);
101103
project.tasks.replace("mlClearModulesDatabase", ClearDHFModulesTask)
102-
project.tasks.replace("mlUpdateIndexes", UpdateIndexes)
104+
project.tasks.replace("mlUpdateIndexes", UpdateIndexes)
103105
project.tasks.mlDeploySecurity.getDependsOn().add("mlDeployRoles");
104106
project.tasks.mlDeploySecurity.getDependsOn().add("mlDeployUsers");
105107
project.tasks.mlUndeploySecurity.getDependsOn().add("mlUndeployRoles");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-2018 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.marklogic.gradle.task
19+
20+
import com.marklogic.hub.deploy.commands.DeployHubPrivilegesCommand
21+
import org.gradle.api.tasks.TaskAction
22+
23+
class DeployHubPrivilegesTask extends HubTask {
24+
25+
@TaskAction
26+
void deployHubPrivileges() {
27+
def cmd = new DeployHubPrivilegesCommand()
28+
cmd.execute(getCommandContext())
29+
}
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.marklogic.gradle.task
2+
3+
import com.marklogic.gradle.task.MarkLogicTask
4+
import org.gradle.api.tasks.TaskAction
5+
6+
class UndeployHubPrivilegesTask extends MarkLogicTask {
7+
8+
@TaskAction
9+
void undeployPrivileges() {
10+
undeployWithCommandWithClassName("DeployHubPrivilegesCommand")
11+
}
12+
}

0 commit comments

Comments
 (0)