Search in sources :

Example 6 with Properties

use of org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties in project accumulo by apache.

the class TracesResource method getScanner.

protected Pair<Scanner, UserGroupInformation> getScanner() throws AccumuloException, AccumuloSecurityException {
    AccumuloConfiguration conf = Monitor.getContext().getConfiguration();
    final boolean saslEnabled = conf.getBoolean(Property.INSTANCE_RPC_SASL_ENABLED);
    UserGroupInformation traceUgi = null;
    final String principal;
    final AuthenticationToken at;
    Map<String, String> loginMap = conf.getAllPropertiesWithPrefix(Property.TRACE_TOKEN_PROPERTY_PREFIX);
    // May be null
    String keytab = loginMap.get(Property.TRACE_TOKEN_PROPERTY_PREFIX.getKey() + "keytab");
    if (keytab == null || keytab.length() == 0) {
        keytab = conf.getPath(Property.GENERAL_KERBEROS_KEYTAB);
    }
    if (saslEnabled && null != keytab) {
        principal = SecurityUtil.getServerPrincipal(conf.get(Property.TRACE_USER));
        try {
            traceUgi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, keytab);
        } catch (IOException e) {
            throw new RuntimeException("Failed to login as trace user", e);
        }
    } else {
        principal = conf.get(Property.TRACE_USER);
    }
    if (!saslEnabled) {
        if (loginMap.isEmpty()) {
            Property p = Property.TRACE_PASSWORD;
            at = new PasswordToken(conf.get(p).getBytes(UTF_8));
        } else {
            Properties props = new Properties();
            int prefixLength = Property.TRACE_TOKEN_PROPERTY_PREFIX.getKey().length();
            for (Entry<String, String> entry : loginMap.entrySet()) {
                props.put(entry.getKey().substring(prefixLength), entry.getValue());
            }
            AuthenticationToken token = Property.createInstanceFromPropertyName(conf, Property.TRACE_TOKEN_TYPE, AuthenticationToken.class, new PasswordToken());
            token.init(props);
            at = token;
        }
    } else {
        at = null;
    }
    final String table = conf.get(Property.TRACE_TABLE);
    Scanner scanner;
    if (null != traceUgi) {
        try {
            scanner = traceUgi.doAs(new PrivilegedExceptionAction<Scanner>() {

                @Override
                public Scanner run() throws Exception {
                    // Make the KerberosToken inside the doAs
                    AuthenticationToken token = at;
                    if (null == token) {
                        token = new KerberosToken();
                    }
                    return getScanner(table, principal, token);
                }
            });
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("Failed to obtain scanner", e);
        }
    } else {
        if (null == at) {
            throw new AssertionError("AuthenticationToken should not be null");
        }
        scanner = getScanner(table, principal, at);
    }
    return new Pair<>(scanner, traceUgi);
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) AuthenticationToken(org.apache.accumulo.core.client.security.tokens.AuthenticationToken) KerberosToken(org.apache.accumulo.core.client.security.tokens.KerberosToken) IOException(java.io.IOException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Properties(org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) Property(org.apache.accumulo.core.conf.Property) AccumuloConfiguration(org.apache.accumulo.core.conf.AccumuloConfiguration) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Pair(org.apache.accumulo.core.util.Pair)

Example 7 with Properties

use of org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties in project accumulo by apache.

the class CreateToken method execute.

@Override
public void execute(String[] args) {
    Opts opts = new Opts();
    opts.parseArgs("accumulo create-token", args);
    Password pass = opts.password;
    if (pass == null && opts.securePassword != null) {
        pass = opts.securePassword;
    }
    try {
        String principal = opts.principal;
        if (principal == null) {
            principal = getConsoleReader().readLine("Username (aka principal): ");
        }
        AuthenticationToken token = Class.forName(opts.tokenClassName).asSubclass(AuthenticationToken.class).newInstance();
        Properties props = new Properties();
        for (TokenProperty tp : token.getProperties()) {
            String input;
            if (pass != null && tp.getKey().equals("password")) {
                input = pass.toString();
            } else {
                if (tp.getMask()) {
                    input = getConsoleReader().readLine(tp.getDescription() + ": ", '*');
                } else {
                    input = getConsoleReader().readLine(tp.getDescription() + ": ");
                }
            }
            props.put(tp.getKey(), input);
            token.init(props);
        }
        String tokenBase64 = Base64.getEncoder().encodeToString(AuthenticationTokenSerializer.serialize(token));
        String tokenFile = opts.tokenFile;
        if (tokenFile == null) {
            tokenFile = getConsoleReader().readLine("File to save auth token to: ");
        }
        File tf = new File(tokenFile);
        if (!tf.exists()) {
            if (!tf.createNewFile()) {
                throw new IOException("Couldn't create " + tf.getCanonicalPath());
            }
        }
        PrintStream out = new PrintStream(new FileOutputStream(tf, true), true, UTF_8.name());
        String outString = principal + ":" + opts.tokenClassName + ":" + tokenBase64;
        out.println(outString);
        out.close();
        System.out.println("Token written to " + tokenFile + ". Remember to upload it to hdfs.");
    } catch (IOException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
Also used : PrintStream(java.io.PrintStream) AuthenticationToken(org.apache.accumulo.core.client.security.tokens.AuthenticationToken) TokenProperty(org.apache.accumulo.core.client.security.tokens.AuthenticationToken.TokenProperty) IOException(java.io.IOException) Properties(org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Password(org.apache.accumulo.core.cli.ClientOpts.Password)

Example 8 with Properties

use of org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties in project accumulo by apache.

the class ClientOpts method getToken.

public AuthenticationToken getToken() {
    if (null != tokenClassName) {
        final Properties props = new Properties();
        if (!loginProps.isEmpty()) {
            for (Entry<String, String> loginOption : loginProps.entrySet()) props.put(loginOption.getKey(), loginOption.getValue());
        }
        // It's expected that the user is already logged in via UserGroupInformation or external to this program (kinit).
        try {
            AuthenticationToken token = Class.forName(tokenClassName).asSubclass(AuthenticationToken.class).newInstance();
            token.init(props);
            return token;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // other token types should have resolved by this point, so return PasswordToken
    Password pass = null;
    if (securePassword != null) {
        pass = securePassword;
    } else if (password != null) {
        pass = password;
    } else {
        try {
            pass = Password.promptUser();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return new PasswordToken(pass.value);
}
Also used : PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) AuthenticationToken(org.apache.accumulo.core.client.security.tokens.AuthenticationToken) IOException(java.io.IOException) Properties(org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException)

Aggregations

Properties (org.apache.accumulo.core.client.security.tokens.AuthenticationToken.Properties)8 IOException (java.io.IOException)4 AuthenticationToken (org.apache.accumulo.core.client.security.tokens.AuthenticationToken)4 Test (org.junit.Test)4 PasswordToken (org.apache.accumulo.core.client.security.tokens.PasswordToken)3 AccumuloException (org.apache.accumulo.core.client.AccumuloException)2 Property (org.apache.accumulo.core.conf.Property)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 Password (org.apache.accumulo.core.cli.ClientOpts.Password)1 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)1 Connector (org.apache.accumulo.core.client.Connector)1 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)1 Scanner (org.apache.accumulo.core.client.Scanner)1 TableExistsException (org.apache.accumulo.core.client.TableExistsException)1 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)1 TokenProperty (org.apache.accumulo.core.client.security.tokens.AuthenticationToken.TokenProperty)1 KerberosToken (org.apache.accumulo.core.client.security.tokens.KerberosToken)1