Merge "Junit test file for AbsCredBody.java"
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / perm / Grant.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright (C) 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.cmd.perm;
25
26 import org.onap.aaf.auth.cmd.AAFcli;
27 import org.onap.aaf.auth.cmd.Cmd;
28 import org.onap.aaf.auth.cmd.Param;
29 import org.onap.aaf.auth.rserv.HttpMethods;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.client.Future;
33 import org.onap.aaf.cadi.client.Rcli;
34 import org.onap.aaf.cadi.client.Retryable;
35 import org.onap.aaf.misc.env.APIException;
36
37 import aaf.v2_0.Pkey;
38 import aaf.v2_0.RolePermRequest;
39
40 /**
41  * 
42  * @author Jonathan
43  *
44  */
45 public class Grant extends Cmd {
46     private static final String[] options = {"grant","ungrant","setTo"};
47
48     public Grant(Perm parent) {
49         super(parent,null,
50             new Param(optionsToString(options),true),
51             new Param("type",true),
52             new Param("instance",true),
53             new Param("action",true),
54             new Param("role[,role]* (!REQ S)",false)
55             ); 
56     }
57
58     @Override
59     public int _exec(final int index, final String ... args) throws CadiException, APIException, LocatorException {
60         return same(new Retryable<Integer>() {
61             @Override
62             public Integer code(Rcli<?> client) throws CadiException, APIException {
63                 int idx = index;
64                 String action = args[idx++];
65                 int option = whichOption(options, action);
66         
67                 RolePermRequest rpr = new RolePermRequest();
68                 Pkey pk = new Pkey();
69                 pk.setType(args[idx++]);
70                 pk.setInstance(args[idx++]);
71                 pk.setAction(args[idx++]);
72                 rpr.setPerm(pk);
73                 setStartEnd(rpr);
74                 
75                 Future<RolePermRequest> frpr = null;
76         
77                 if (option != 2) {
78                     String[] roles = args[idx++].split(",");
79                     String strA;
80                     String strB;
81                     for (String role : roles) {
82                         rpr.setRole(role);
83                         if (option==0) {
84                             // You can request to Grant Permission to a Role
85                             setQueryParamsOn(client);
86                             frpr = client.create(
87                                     "/authz/role/perm", 
88                                     getDF(RolePermRequest.class),
89                                     rpr
90                                     );
91                             strA = "Granted Permission [";
92                             strB = "] to Role [";
93                         } else {
94                             // You can request to UnGrant Permission to a Role
95                             setQueryParamsOn(client);
96                             frpr = client.delete(
97                                     "/authz/role/" + role + "/perm", 
98                                     getDF(RolePermRequest.class),
99                                     rpr
100                                     );
101                             strA = "UnGranted Permission [";
102                             strB = "] from Role [";
103                         }
104                         if (frpr.get(AAFcli.timeout())) {
105                             pw().println(strA + pk.getType() + '|' + pk.getInstance() + '|' + pk.getAction() 
106                                     + strB + role +']');
107                         } else {
108                             if (frpr.code()==202) {
109                                 pw().print("Permission Role ");
110                                 pw().print(option==0?"Granted":"Ungranted");
111                                 pw().println(" Accepted, but requires Approvals before actualizing");
112                             } else {
113                                 error(frpr);
114                                 idx=Integer.MAX_VALUE;
115                             }            
116                         }
117                     }
118                 } else {
119                     String allRoles = "";
120                     if (idx < args.length) 
121                         allRoles = args[idx++];
122                         
123                     rpr.setRole(allRoles);
124                     frpr = client.update(
125                             "/authz/role/perm", 
126                             getDF(RolePermRequest.class), 
127                             rpr);
128                     if (frpr.get(AAFcli.timeout())) {
129                         pw().println("Set Permission's Roles to [" + allRoles + "]");
130                     } else {
131                         error(frpr);
132                     }            
133                 } 
134                 return frpr==null?0:frpr.code();
135             }
136         });
137     }
138
139     @Override
140     public void detailedHelp(int indent, StringBuilder sb) {
141         detailLine(sb,indent,"Grant a Permission to a Role or Roles  OR");
142         detailLine(sb,indent,"Ungrant a Permission from a Role or Roles  OR");
143         detailLine(sb,indent,"Set a Permission's roles to roles supplied.");
144         detailLine(sb,indent+4,"WARNING: Roles supplied with setTo will be the ONLY roles attached to this permission");
145         detailLine(sb,indent+8,"If no roles are supplied, permission's roles are reset.");
146         detailLine(sb,indent,"see Create for definitions of type,instance and action");
147         api(sb,indent,HttpMethods.POST,"authz/role/perm",RolePermRequest.class,true);
148         api(sb,indent,HttpMethods.DELETE,"authz/role/<role>/perm",RolePermRequest.class,false);
149         api(sb,indent,HttpMethods.PUT,"authz/role/perm",RolePermRequest.class,false);
150
151     }
152
153 }