View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.util.shell.commands;
18  
19  import java.io.IOException;
20  import java.nio.ByteBuffer;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.accumulo.core.client.AccumuloException;
25  import org.apache.accumulo.core.client.AccumuloSecurityException;
26  import org.apache.accumulo.core.security.CredentialHelper;
27  import org.apache.accumulo.core.security.thrift.Credential;
28  import org.apache.accumulo.core.security.tokens.PasswordToken;
29  import org.apache.accumulo.core.util.shell.Shell;
30  import org.apache.accumulo.core.util.shell.Shell.Command;
31  import org.apache.accumulo.core.util.shell.Token;
32  import org.apache.commons.cli.CommandLine;
33  
34  public class UserCommand extends Command {
35    public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, IOException {
36      // save old credentials and connection in case of failure
37      String user = cl.getArgs()[0];
38      byte[] pass;
39      
40      // We can't let the wrapping try around the execute method deal
41      // with the exceptions because we have to do something if one
42      // of these methods fails
43      final String p = shellState.readMaskedLine("Enter password for user " + user + ": ", '*');
44      if (p == null) {
45        shellState.getReader().printNewline();
46        return 0;
47      } // user canceled
48      pass = p.getBytes();
49      shellState.updateUser(CredentialHelper.create(user, new PasswordToken().setPassword(pass), shellState.getConnector().getInstance().getInstanceID()));
50      return 0;
51    }
52    
53    @Override
54    public String description() {
55      return "switches to the specified user";
56    }
57    
58    @Override
59    public void registerCompletion(final Token root, final Map<Command.CompletionSet,Set<String>> special) {
60      registerCompletionForUsers(root, special);
61    }
62    
63    @Override
64    public String usage() {
65      return getName() + " <username>";
66    }
67    
68    @Override
69    public int numArgs() {
70      return 1;
71    }
72  }