ca

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

- @Author: jffan - @Date: 2024-08-05 09:34:20 - @LastEditTime: 2024-08-14 16:39:35 - @LastEditors: jffan - @FilePath: \tcas-cli\cmd\ca\ca.go - @Description: Command to obtain a CA certificate

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:                        "ca",
	Short:                      "get root CA",
	Long:                       "get root CA",
	SuggestionsMinimumDistance: 1,
	DisableSuggestions:         false,
	Run: func(cmd *cobra.Command, args []string) {
		url, _ := cmd.Flags().GetString("url")
		logrus.Debugf("ca url " + consts.ColorGreen + url + consts.OutReset)
		ouputPath, _ := cmd.Flags().GetString("output")
		logrus.Debugf(consts.ColorYellow + "Save the file in the path (`" + ouputPath + "`)" + consts.OutReset)
		err := file.EnsureDirExists(ouputPath)
		if err != nil {
			logrus.Errorf("Error ensuring directory exists: %v", err)
			return
		}

		tcasConfig, err := initconfig.ReadTCASInfoFromConfig(url)
		if err != nil {
			logrus.Errorf("get tcasConfig failed, please run <tcascli init> first")
			return
		}
		m, err := manager.New(tcasConfig, tees.GetCollectors())
		if err != nil {
			logrus.Errorf("create attest manager failed, error: %s", err)
			return
		}
		res, err := m.GetRootCert()
		if err != nil {
			logrus.Errorf("Request get root ca failed: %v", err)
			return
		}
		if res != nil && len(res.Keys) > 0 && len(res.Keys[0].X5c) > 0 {
			pemData, err := manager.X5cToCertPem(res.Keys[0].X5c)
			if err != nil {
				logrus.Errorf(err.Error())
				return
			}
			fileName := fmt.Sprintf("ca%s.pem", res.Keys[0].Kid)
			resultPath := filepath.Join(ouputPath, fileName)
			err = os.WriteFile(resultPath, []byte(pemData.String()), 0644)
			if err != nil {
				logrus.Errorf("Error writing PEM file: %v", err)
				return
			}
			fmt.Println(consts.ColorGreen + "The root ca save in: " + resultPath + consts.OutReset)
		} else {
			logrus.Errorf("No certificate found")
		}

	},
}

Cmd represents the ca command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL