Adjust Agent for none K8s
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / Cred.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd.user;
23
24 import java.util.List;
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.aaf.client.ErrMessage;
33 import org.onap.aaf.cadi.client.Future;
34 import org.onap.aaf.cadi.client.Rcli;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.misc.env.APIException;
37
38 import aaf.v2_0.CredRequest;
39 import aaf.v2_0.Error;
40
41 public class Cred extends Cmd {
42     public static final String ATTEMPT_FAILED_SPECIFICS_WITHELD = "Attempt Failed.  Specifics witheld.";
43     private static final String CRED_PATH = "/authn/cred";
44     private static final String[] options = {"add","del","reset","extend"/*,"clean"*/};
45         private ErrMessage em;
46 //      private RosettaDF<Error> errDF;
47     public Cred(User parent) throws APIException {
48         super(parent,"cred",
49                 new Param(optionsToString(options),true),
50                 new Param("id",true),
51                 new Param("password (! D|E)",false),
52                 new Param("entry# (if multi)",false)
53         );
54         em = new ErrMessage(aafcli.env());
55     }
56
57     @Override
58     public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException { 
59         int idx = _idx;
60         String key = args[idx++];
61         final int option = whichOption(options,key);
62
63         final CredRequest cr = new CredRequest();
64         cr.setId(args[idx++]);
65         if (option!=1 && option!=3) {
66             if (idx>=args.length) throw new CadiException("Password Required");
67             cr.setPassword(args[idx++]);
68         }
69         if (args.length>idx) {
70             cr.setEntry(args[idx]);
71         }
72         
73         // Set Start/End commands
74         setStartEnd(cr);
75         Integer ret = same(new Retryable<Integer>() {
76             @Override
77             public Integer code(Rcli<?> client) throws CadiException, APIException {
78                 Future<CredRequest> fp=null;
79                 String verb =null;
80                 switch(option) {
81                     case 0:
82                         fp = client.create(
83                             CRED_PATH, 
84                             getDF(CredRequest.class), 
85                             cr
86                             );
87                         verb = "Added Credential [";
88                         break;
89                     case 1:
90                         setQueryParamsOn(client);
91                         fp = client.delete(CRED_PATH,
92                             getDF(CredRequest.class),
93                             cr
94                             );
95                         verb = "Deleted Credential [";
96                         break;
97                     case 2:
98                         fp = client.update(
99                             CRED_PATH,
100                             getDF(CredRequest.class),
101                             cr
102                             );
103                         verb = "Reset Credential [";
104                         break;
105                     case 3:
106                         fp = client.update(
107                             CRED_PATH+"/5",
108                             getDF(CredRequest.class),
109                             cr
110                             );
111                         verb = "Extended Credential [";
112                         break;
113                     default:
114                         break;
115                 }
116                 if (fp==null) {
117                     return null; // get by Sonar check.
118                 }
119                 if (fp.get(AAFcli.timeout())) {
120                     pw().print(verb);
121                     pw().print(cr.getId());
122                     pw().println(']');
123                 } else if (fp.code()==202) {
124                         pw().println("Credential Action Accepted, but requires Approvals before actualizing");
125                 } else if (fp.code()==300 || fp.code()==406) {
126                         Error err = em.getError(fp);
127                         String text = err.getText();
128                         List<String> vars = err.getVariables();
129                         
130                         // IMPORTANT! We do this backward, because it is looking for string
131                         // %1 or %13.  If we replace %1 first, that messes up %13
132                         for(int i=vars.size()-1;i>0;--i) {
133                                 text = text.replace("%"+(i+1), (i<10?" ":"") + i+") " + vars.get(i));
134                         }
135
136                         text = text.replace("%1",vars.get(0));
137                         pw().println(text);
138                 } else if (fp.code()==406 && option==1) {
139                         pw().println("You cannot delete this Credential");
140                 } else if (fp.code()==409 && option==0) {
141                     pw().println("You cannot add two Passwords for same day");
142                 } else {
143                     pw().println(ATTEMPT_FAILED_SPECIFICS_WITHELD);
144                 }
145                 return fp.code();
146             }
147         });
148         if (ret==null)ret = -1;
149         return ret;
150     }
151     
152     @Override
153     public void detailedHelp(int _indent, StringBuilder sb) {
154             int indent = _indent;
155         detailLine(sb,indent,"Add, Delete or Reset Credential");
156         indent+=2;
157         detailLine(sb,indent,"id       - the ID to create/delete/reset within AAF");
158         detailLine(sb,indent,"password - Company Policy compliant Password (not required for Delete)");
159         detailLine(sb,indent,"entry    - selected option when deleting/resetting a cred with multiple entries");
160         sb.append('\n');
161         detailLine(sb,indent,"The Domain can be related to any Namespace you have access to *");
162         detailLine(sb,indent,"The Domain is in reverse order of Namespace, i.e. ");
163         detailLine(sb,indent+2,"NS of com.att.myapp can create user of XY1234@myapp.att.com");
164         sb.append('\n');
165         detailLine(sb,indent,"NOTE: AAF does support multiple creds with the same ID. Check with your org if you");
166         detailLine(sb,indent+2,"have this implemented. (For example, this is implemented for MechIDs at AT&T)");
167         sb.append('\n');            
168         detailLine(sb,indent,"*NOTE: com.att.csp is a reserved Domain for Global Sign On");
169
170         detailLine(sb,indent,"Delegates can be listed by the User or by the Delegate");
171         indent-=2;
172         api(sb,indent,HttpMethods.POST,"authn/cred",CredRequest.class,true);
173         api(sb,indent,HttpMethods.DELETE,"authn/cred",CredRequest.class,false);
174         api(sb,indent,HttpMethods.PUT,"authn/cred",CredRequest.class,false);
175     }
176 }