2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.cmd.perm;
24 import org.onap.aaf.auth.cmd.AAFcli;
25 import org.onap.aaf.auth.cmd.Cmd;
26 import org.onap.aaf.auth.cmd.Param;
27 import org.onap.aaf.auth.rserv.HttpMethods;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.client.Future;
31 import org.onap.aaf.cadi.client.Rcli;
32 import org.onap.aaf.cadi.client.Retryable;
33 import org.onap.aaf.misc.env.APIException;
36 import aaf.v2_0.RolePermRequest;
43 public class Grant extends Cmd {
44 private final static String[] options = {"grant","ungrant","setTo"};
46 public Grant(Perm parent) {
48 new Param(optionsToString(options),true),
49 new Param("type",true),
50 new Param("instance",true),
51 new Param("action",true),
52 new Param("role[,role]* (!REQ S)",false)
57 public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
58 return same(new Retryable<Integer>() {
60 public Integer code(Rcli<?> client) throws CadiException, APIException {
62 String action = args[idx++];
63 int option = whichOption(options, action);
65 RolePermRequest rpr = new RolePermRequest();
67 pk.setType(args[idx++]);
68 pk.setInstance(args[idx++]);
69 pk.setAction(args[idx++]);
73 Future<RolePermRequest> frpr = null;
76 String[] roles = args[idx++].split(",");
78 for(String role : roles) {
81 // You can request to Grant Permission to a Role
82 setQueryParamsOn(client);
85 getDF(RolePermRequest.class),
88 strA = "Granted Permission [";
91 // You can request to UnGrant Permission to a Role
92 setQueryParamsOn(client);
94 "/authz/role/" + role + "/perm",
95 getDF(RolePermRequest.class),
98 strA = "UnGranted Permission [";
99 strB = "] from Role [";
101 if(frpr.get(AAFcli.timeout())) {
102 pw().println(strA + pk.getType() + '|' + pk.getInstance() + '|' + pk.getAction()
105 if (frpr.code()==202) {
106 pw().print("Permission Role ");
107 pw().print(option==0?"Granted":"Ungranted");
108 pw().println(" Accepted, but requires Approvals before actualizing");
111 idx=Integer.MAX_VALUE;
116 String allRoles = "";
117 if (idx < args.length)
118 allRoles = args[idx++];
120 rpr.setRole(allRoles);
121 frpr = client.update(
123 getDF(RolePermRequest.class),
125 if(frpr.get(AAFcli.timeout())) {
126 pw().println("Set Permission's Roles to [" + allRoles + "]");
131 return frpr==null?0:frpr.code();
137 public void detailedHelp(int indent, StringBuilder sb) {
138 detailLine(sb,indent,"Grant a Permission to a Role or Roles OR");
139 detailLine(sb,indent,"Ungrant a Permission from a Role or Roles OR");
140 detailLine(sb,indent,"Set a Permission's roles to roles supplied.");
141 detailLine(sb,indent+4,"WARNING: Roles supplied with setTo will be the ONLY roles attached to this permission");
142 detailLine(sb,indent+8,"If no roles are supplied, permission's roles are reset.");
143 detailLine(sb,indent,"see Create for definitions of type,instance and action");
144 api(sb,indent,HttpMethods.POST,"authz/role/perm",RolePermRequest.class,true);
145 api(sb,indent,HttpMethods.DELETE,"authz/role/<role>/perm",RolePermRequest.class,false);
146 api(sb,indent,HttpMethods.PUT,"authz/role/perm",RolePermRequest.class,false);