You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2020/12/16 15:12:13 UTC

[GitHub] [ozone] adoroszlai commented on a change in pull request #1662: HDDS-4507. Add SCM CA CLI to query certificate.

adoroszlai commented on a change in pull request #1662:
URL: https://github.com/apache/ozone/pull/1662#discussion_r544359983



##########
File path: hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/cert/ListSubcommand.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.hdds.scm.cli.cert;
+
+import java.io.IOException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import java.util.List;
+
+import org.apache.hadoop.hdds.cli.HddsVersionProvider;
+import org.apache.hadoop.hdds.protocol.SCMSecurityProtocol;
+
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+import org.apache.hadoop.hdds.security.x509.certificate.utils.CertificateCodec;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Help.Visibility;
+import picocli.CommandLine.Option;
+
+/**
+ * This is the handler that process certificate list command.
+ */
+@Command(
+    name = "list",
+    description = "List certificates",
+    mixinStandardHelpOptions = true,
+    versionProvider = HddsVersionProvider.class)
+public class ListSubcommand extends ScmCertSubcommand {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(ListSubcommand.class);
+
+  @Option(names = {"-s", "--start"},
+      description = "Certificate serial id to start the iteration",
+      defaultValue = "0", showDefaultValue = Visibility.ALWAYS)
+  private long startSerialId;
+
+  @Option(names = {"-c", "--count"},
+      description = "Maximum number of certificates to list",
+      defaultValue = "20", showDefaultValue = Visibility.ALWAYS)
+  private int count;
+
+  @Option(names = {"-r", "--role"},
+      description = "Filter certificate by the role: om/datanode",
+      defaultValue = "datanode", showDefaultValue = Visibility.ALWAYS)
+  private String role;
+
+  @Option(names = {"-t", "--type"},
+      description = "Filter certificate by the type: valid or revoked",
+      defaultValue = "valid", showDefaultValue = Visibility.ALWAYS)
+  private String type;
+
+  private HddsProtos.NodeType parseCertRole(String r) {
+    if (r.equalsIgnoreCase("om")) {
+      return HddsProtos.NodeType.OM;
+    } else if (r.equalsIgnoreCase("scm")) {
+      return HddsProtos.NodeType.SCM;
+    } else {
+      return HddsProtos.NodeType.DATANODE;
+    }
+  }
+
+  private void printCert(X509Certificate cert) {
+    LOG.info("{}\t{}\t{}\t{}", cert.getSerialNumber(), cert.getNotBefore(),
+        cert.getNotAfter(), cert.getSubjectDN());
+  }
+
+  @Override
+  protected void execute(SCMSecurityProtocol client) throws IOException {
+    boolean isRevoked = type.equalsIgnoreCase("revoked");
+    List<String> certPemList = client.listCertificate(
+        parseCertRole(role), startSerialId, count, isRevoked);
+    LOG.info("Total {} {} certificates: ", certPemList.size(), type);
+    LOG.info("SerialNumber\t\tValid From\t\tExpiry\t\tSubject");

Review comment:
       Headers and data don't really line up:
   
   ```
   SerialNumber		Valid From		Expiry		Subject
   2205019573485754	Wed Dec 16 00:00:00 UTC 2020	Thu Dec 16 00:00:00 UTC 2021	O=CID-fe48b3e3-f808-4cbd-86a9-847465f6f617, OU=b4fb9f43-c2ad-454a-b204-ba13d05bff63, CN=root@55967653af5c
   ```
   
   should be something like:
   
   ```
   SerialNumber		Valid From			Expiry				Subject
   2205019573485754	Wed Dec 16 00:00:00 UTC 2020	Thu Dec 16 00:00:00 UTC 2021	O=CID-fe48b3e3-f808-4cbd-86a9-847465f6f617, OU=b4fb9f43-c2ad-454a-b204-ba13d05bff63, CN=root@55967653af5c
   ```

##########
File path: hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/cert/InfoSubcommand.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.hdds.scm.cli.cert;
+
+import java.io.IOException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import com.google.common.base.Preconditions;
+import org.apache.hadoop.hdds.cli.GenericParentCommand;
+import org.apache.hadoop.hdds.cli.HddsVersionProvider;
+import org.apache.hadoop.hdds.protocol.SCMSecurityProtocol;
+
+import org.apache.hadoop.hdds.security.x509.certificate.utils.CertificateCodec;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import picocli.CommandLine.Command;
+import picocli.CommandLine.Model.CommandSpec;
+import picocli.CommandLine.Parameters;
+import picocli.CommandLine.Spec;
+
+/**
+ * This is the handler that process certificate info command.
+ */
+@Command(
+    name = "info",
+    description = "Show detailed information for a specific certificate",
+    mixinStandardHelpOptions = true,
+    versionProvider = HddsVersionProvider.class)
+
+class InfoSubcommand extends ScmCertSubcommand {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(InfoSubcommand.class);
+
+  @Spec
+  private CommandSpec spec;
+
+  @Parameters(description = "Serial id of the certificate in decimal.")
+  private String serialId;
+
+  @Override
+  public void execute(SCMSecurityProtocol client) throws IOException {
+    final String certPemStr =
+        client.getCertificate(serialId);
+    Preconditions.checkNotNull(certPemStr,
+        "Certificate can't be found");
+
+    // Print container report info.
+    LOG.info("Certificate id: {}", serialId);
+    boolean verbose = spec.root().userObject() instanceof GenericParentCommand
+        && ((GenericParentCommand) spec.root().userObject()).isVerbose();

Review comment:
       `verbose` is unused.

##########
File path: hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/authority/MockCAStore.java
##########
@@ -51,4 +54,11 @@ public X509Certificate getCertificateByID(BigInteger serialID,
       throws IOException {
     return null;
   }
+
+  @Override
+  public List<X509Certificate> listCertificate(HddsProtos.NodeType role,
+      BigInteger startSerialID, int count, CertType certType)
+      throws IOException {
+    return null;

Review comment:
       Should we return `emptyList()` instead?

##########
File path: hadoop-ozone/dist/src/main/smoketest/security/admin-cert.robot
##########
@@ -0,0 +1,44 @@
+# 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 for ozone admin cert command
+Library             BuiltIn
+Library             String
+Resource            ../commonlib.robot
+Resource            ../lib/os.robot
+Resource            ../ozone-lib/shell.robot
+Suite Setup         Setup Test
+Test Timeout        5 minutes
+
+*** Variables ***
+
+*** Keywords ***
+Setup Test
+    Run Keyword     Kinit test user     testuser     testuser.keytab
+
+*** Test Cases ***
+List valid certificates
+    ${output} =      Execute    ozone admin cert list
+                     Should Contain    ${output}    valid certificates
+
+List revoked certificates
+    ${output} =      Execute    ozone admin cert list -t revoked
+                     Should Contain    ${output}    Total 0 revoked certificates
+
+Info of the cert
+    ${output} =      Execute   for id in $(ozone admin cert list |grep UTC|awk '{print $1}'); do ozone admin cert info $id; done

Review comment:
       Nit: would it be enough to check only the first certificate to save a few seconds?
   
   ```
   ozone admin cert list |grep UTC|awk '{print $1}'| head -1 | xargs ozone admin cert info
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org