From commits-return-18193-archive-asf-public=cust-asf.ponee.io@pulsar.apache.org Tue Nov 27 10:40:01 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 0558B180677 for ; Tue, 27 Nov 2018 10:40:00 +0100 (CET) Received: (qmail 82522 invoked by uid 500); 27 Nov 2018 09:40:00 -0000 Mailing-List: contact commits-help@pulsar.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pulsar.apache.org Delivered-To: mailing list commits@pulsar.apache.org Received: (qmail 82513 invoked by uid 99); 27 Nov 2018 09:40:00 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Nov 2018 09:40:00 +0000 From: GitBox To: commits@pulsar.apache.org Subject: [GitHub] ivankelly commented on a change in pull request #2888: PIP-25: Token based authentication Message-ID: <154331159962.9674.12622469696576050854.gitbox@gitbox.apache.org> Date: Tue, 27 Nov 2018 09:39:59 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit ivankelly commented on a change in pull request #2888: PIP-25: Token based authentication URL: https://github.com/apache/pulsar/pull/2888#discussion_r236583781 ########## File path: pulsar-broker/src/main/java/org/apache/pulsar/utils/auth/tokens/TokensCliUtils.java ########## @@ -0,0 +1,231 @@ +/** + * 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. + */ +package org.apache.pulsar.utils.auth.tokens; + +import com.beust.jcommander.JCommander; +import com.beust.jcommander.Parameter; +import com.beust.jcommander.Parameters; +import com.google.common.base.Charsets; + +import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.io.Decoders; +import io.jsonwebtoken.io.Encoders; +import io.jsonwebtoken.security.Keys; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.Key; +import java.security.KeyPair; +import java.util.Date; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +import javax.crypto.SecretKey; + +import lombok.Cleanup; + +import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils; +import org.apache.pulsar.common.util.RelativeTimeUtil; + +public class TokensCliUtils { + + public static class Arguments { + @Parameter(names = { "-h", "--help" }, description = "Show this help message") + private boolean help = false; + } + + @Parameters(commandDescription = "Create a new secret key") + public static class CommandCreateSecretKey { + @Parameter(names = { "-a", + "--signature-algorithm" }, description = "The signature algorithm for the new secret key.") + SignatureAlgorithm algorithm = SignatureAlgorithm.HS256; + + @Parameter(names = { "-o", + "--output" }, description = "Write the secret key to a file instead of stdout") + String outputFile; + + @Parameter(names = { + "-b", "--base-64" }, description = "Encode the key in base64") + boolean base64 = false; + + public void run() throws IOException { + SecretKey secretKey = AuthTokenUtils.createSecretKey(algorithm); + byte[] encoded = secretKey.getEncoded(); + + if (base64) { + encoded = Encoders.BASE64.encode(encoded).getBytes(); + } + + if (outputFile != null) { + Files.write(Paths.get(outputFile), encoded); + } else { + System.out.write(encoded); + } + } + } + + @Parameters(commandDescription = "Create a new or pair of keys public/private") + public static class CommandCreateKeyPair { + @Parameter(names = { "-a", + "--signature-algorithm" }, description = "The signature algorithm for the new key pair.") + SignatureAlgorithm algorithm = SignatureAlgorithm.RS256; + + @Parameter(names = { + "--output-private-key" }, description = "File where to write the private key", required = true) + String privateKeyFile; + @Parameter(names = { + "--output-public-key" }, description = "File where to write the public key", required = true) + String publicKeyFile; + + public void run() throws IOException { + KeyPair pair = Keys.keyPairFor(algorithm); + + Files.write(Paths.get(publicKeyFile), pair.getPublic().getEncoded()); + Files.write(Paths.get(privateKeyFile), pair.getPrivate().getEncoded()); + } + } + + @Parameters(commandDescription = "Create a new token") + public static class CommandCreateToken { + + @Parameter(names = { "-s", + "--subject" }, description = "Specify the 'subject' or 'principal' associate with this token", required = true) + private String subject; + + @Parameter(names = { "-e", + "--expiry-time" }, description = "Relative expiry time for the token (eg: 1h, 3d, 10y). (m=minutes) Default: no expiration") + private String expiryTime; + + @Parameter(names = { "-pk", + "--is-private-key" }, description = "Indicate the signing key is a private key (rather than a symmetric secret key)") + private Boolean isPrivateKey = false; + + @Parameter(names = { "-k", + "--signing-key" }, description = "Pass the signing key. This can either be: data:, file:, etc..", required = true) + private String key; + + public void run() throws Exception { + byte[] encodedKey = AuthTokenUtils.readKeyFromUrl(key); + + Key signingKey; + + if (isPrivateKey) { + signingKey = AuthTokenUtils.decodePrivateKey(encodedKey); + } else { + signingKey = AuthTokenUtils.decodeSecretKey(encodedKey); + } + + Optional optExpiryTime = Optional.empty(); + if (expiryTime != null) { + long relativeTimeMillis = TimeUnit.SECONDS + .toMillis(RelativeTimeUtil.parseRelativeTimeInSeconds(expiryTime)); + optExpiryTime = Optional.of(new Date(System.currentTimeMillis() + relativeTimeMillis)); + } + + String token = AuthTokenUtils.createToken(signingKey, subject, optExpiryTime); + System.out.println(token); + } + } + + @Parameters(commandDescription = "Show the content of token") + public static class CommandShowToken { + + @Parameter(description = "The token string", arity = 1) + private java.util.List args; + + @Parameter(names = { "-i", + "--stdin" }, description = "Read token from standard input") + private Boolean stdin = false; + + @Parameter(names = { "-f", + "--token-file" }, description = "Read tokn from a file") + private String tokenFile; + + public void run() throws Exception { + String token; + if (args != null) { + token = args.get(0); + } else if (stdin) { Review comment: sure ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services