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



##########
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:
       Fixed.
   ```
   Total 2 valid certificates: 
   SerialNumber      Valid From                     Expiry                      
   Subject                                                                      
                                 
   170901133690402   Sun Dec 20 00:00:00 UTC 2020   Mon Dec 20 00:00:00 UTC 
2021   O=CID-4b97ad93-f91c-4700-83b8-62df6a24e12a, 
OU=e25f8f9c-9292-4731-8406-4d0e5e522757, CN=root@a65bf462da45     
   170904253373102   Sun Dec 20 00:00:00 UTC 2020   Mon Dec 20 00:00:00 UTC 
2021   O=CID-4b97ad93-f91c-4700-83b8-62df6a24e12a, 
OU=e25f8f9c-9292-4731-8406-4d0e5e522757, CN=root@om       
   ```




----------------------------------------------------------------
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to