Skip to content

Commit

Permalink
HDDS-3360. Duplicate Ozone Client in renew/cancel token handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai committed Apr 9, 2020
1 parent 1f2255b commit 480b467
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 50 deletions.
@@ -0,0 +1,40 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

*** Settings ***
Documentation Test token operations
Library OperatingSystem
Library String
Library BuiltIn
Resource ../commonlib.robot
Test Timeout 5 minutes

*** Test Cases ***
Get Token
Execute ozone sh token get > /tmp/token.txt
File Should Not Be Empty /tmp/token.txt

Print Token
${output} = Execute ozone sh token print
Should Not Be Empty ${output}

Renew Token
${output} = Execute ozone sh token renew
Should contain ${output} Token renewed successfully

Cancel Token
${output} = Execute ozone sh token cancel
Should contain ${output} Token canceled successfully

Expand Up @@ -58,15 +58,25 @@ public OzoneConfiguration createOzoneConfiguration() {
return parent.createOzoneConfiguration();
}

protected abstract OzoneAddress getAddress() throws OzoneClientException;
protected OzoneAddress getAddress() throws OzoneClientException {
return new OzoneAddress();
}

protected abstract void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException;

protected boolean isApplicable() {
return true;
}

@Override
public Void call() throws Exception {
conf = createOzoneConfiguration();

if (!isApplicable()) {
return null;
}

OzoneAddress address = getAddress();
try (OzoneClient client = createClient(address)) {
if (isVerbose()) {
Expand Down
Expand Up @@ -20,12 +20,7 @@

import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneClientException;
import org.apache.hadoop.ozone.client.OzoneClientFactory;
import org.apache.hadoop.ozone.security.OzoneTokenIdentifier;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.ozone.shell.Handler;
import org.apache.hadoop.security.token.Token;
import picocli.CommandLine;
import picocli.CommandLine.Command;

import java.io.IOException;
Expand All @@ -35,27 +30,12 @@
*/
@Command(name = "cancel",
description = "cancel a delegation token.")
public class CancelTokenHandler extends Handler {

@CommandLine.Mixin
private TokenOption tokenFile;

@Override
protected OzoneAddress getAddress() throws OzoneClientException {
return new OzoneAddress();
}
public class CancelTokenHandler extends TokenHandler {

@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException {

if (securityEnabled("token cancel") && tokenFile.exists()) {
Token<OzoneTokenIdentifier> token = tokenFile.decode();
try (OzoneClient ozoneClient = OzoneClientFactory.getOzoneClient(
getConf(), token)) {
ozoneClient.getObjectStore().cancelDelegationToken(token);
out().printf("Token canceled successfully.%n");
}
}
client.getObjectStore().cancelDelegationToken(getToken());
out().printf("Token canceled successfully.%n");
}
}
Expand Up @@ -39,7 +39,7 @@
description = "get a delegation token.")
public class GetTokenHandler extends Handler {

@CommandLine.Parameters(arity = "1..1",
@CommandLine.Parameters(arity = "0..1",
description = Shell.OZONE_URI_DESCRIPTION)
private String uri;

Expand Down
Expand Up @@ -20,12 +20,7 @@

import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneClientException;
import org.apache.hadoop.ozone.client.OzoneClientFactory;
import org.apache.hadoop.ozone.security.OzoneTokenIdentifier;
import org.apache.hadoop.ozone.shell.Handler;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.security.token.Token;
import picocli.CommandLine;
import picocli.CommandLine.Command;

import java.io.IOException;
Expand All @@ -35,29 +30,12 @@
*/
@Command(name = "renew",
description = "renew a delegation token.")
public class RenewTokenHandler extends Handler {

@CommandLine.Mixin
private TokenOption tokenFile;

@Override
protected OzoneAddress getAddress() throws OzoneClientException {
return new OzoneAddress();
}
public class RenewTokenHandler extends TokenHandler {

@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException, OzoneClientException {

if (securityEnabled("token renew") && tokenFile.exists()) {
Token<OzoneTokenIdentifier> token = tokenFile.decode();
try (OzoneClient ozoneClient = OzoneClientFactory.getOzoneClient(
getConf(), token)) {
long expiryTime = ozoneClient.getObjectStore()
.renewDelegationToken(token);
out().printf("Token renewed successfully, expiry time: %s.%n",
expiryTime);
}
}
long expiryTime = client.getObjectStore().renewDelegationToken(getToken());
out().printf("Token renewed successfully, expiry time: %s.%n", expiryTime);
}
}
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.ozone.shell.token;

import org.apache.hadoop.ozone.client.OzoneClient;
import org.apache.hadoop.ozone.client.OzoneClientFactory;
import org.apache.hadoop.ozone.security.OzoneTokenIdentifier;
import org.apache.hadoop.ozone.shell.Handler;
import org.apache.hadoop.ozone.shell.OzoneAddress;
import org.apache.hadoop.security.token.Token;
import picocli.CommandLine;

import java.io.IOException;

/**
* Handler for requests with an existing token.
*/
public abstract class TokenHandler extends Handler {

@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@CommandLine.Mixin
private TokenOption tokenFile;
private Token<OzoneTokenIdentifier> token;

@Override
protected boolean isApplicable() {
return securityEnabled(spec.qualifiedName()) && tokenFile.exists();
}

@Override
protected OzoneClient createClient(OzoneAddress address)
throws IOException {
token = tokenFile.decode();
return OzoneClientFactory.getOzoneClient(getConf(), token);
}

Token<OzoneTokenIdentifier> getToken() {
return token;
}
}

0 comments on commit 480b467

Please sign in to comment.