getpdf.go 3.34 KB
Newer Older
1
// Copyright © 2018 Heiko Reese <heiko.reese@kit.edu>
Heiko Reese's avatar
Heiko Reese committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Licensed 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 cmd

import (
	"bytes"
	"git.scc.kit.edu/KIT-CA/dfnpki"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"io/ioutil"
	"log"
	"strings"
	"text/template"
)

var (
	getPdfData struct {
		RequestID   string
		RequestFile string
	}
)

// getpdfCmd represents the getpdf command
var getpdfCmd = &cobra.Command{
	Use:   "getpdf",
	Short: "Download pdf document for certificate request",
	// Long: ``,
	Run: func(cmd *cobra.Command, args []string) {
		var (
			RequestID string = "NONE"
			filename  string
		)
		viper.BindPFlags(cmd.PersistentFlags())
		viper.Unmarshal(&getPdfData)

		// handle --requestid
		if viper.IsSet("requestid") {
			RequestID = viper.GetString("requestid")
		}
		// handle --requestidfile
		if viper.IsSet("requestidfile") {
			content, err := ioutil.ReadFile(viper.GetString("requestidfile"))
			if err != nil {
				log.Fatal(err)
			}
			tokens := strings.Fields(string(content))
			if len(tokens) < 1 {
				log.Fatalf("No request id found in %s", viper.GetString("requestidfile"))
			}
			RequestID = strings.TrimSpace(tokens[0])
		}
		if RequestID == "NONE" {
			log.Fatal("Please specify a request id")
		}

		// create http client
Heiko Reese's avatar
lint    
Heiko Reese committed
69
		httpclient, err := dfnpki.GetPublicHTTPClient()
Heiko Reese's avatar
Heiko Reese committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		if err != nil {
			log.Fatal(err)
		}
		client := dfnpki.NewSoapClient(httpclient, dfnpki.GeneratePublicURL(viper.GetString("ca")))

		// generate output filename
		filenameTemplate, err := template.New("outfile").Parse(viper.GetString("outfile"))
		if err != nil {
			// last chance fallback…
			filename = viper.GetString("outfile")
		}
		outfilewriter := bytes.Buffer{}
		err = filenameTemplate.Execute(&outfilewriter, getPdfData)
		if err != nil {
			// last chance fallback…
			filename = viper.GetString("outfile")
		}

		pdf, err := dfnpki.GetRequestPrintout(client, viper.GetInt("ra-id"), getPdfData.RequestID, viper.GetString("pin"))
		if err != nil {
			log.Fatalf("Unable to retrieve PDF: %s", err)
		}
Heiko Reese's avatar
lint    
Heiko Reese committed
92
		err = ioutil.WriteFile(filename, pdf, 0644)
Heiko Reese's avatar
Heiko Reese committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		if err != nil {
			log.Fatalf("Unable to write PDF file »%s«: %s", filename, err)
		}
		log.Printf("PDF request saved to »%s«", filename)
	},
}

func init() {
	rootCmd.AddCommand(getpdfCmd)

	getpdfCmd.PersistentFlags().SortFlags = false
	getpdfCmd.PersistentFlags().StringVar(&getPdfData.RequestID, "requestid", "", "Request id (»Seriennummer«)")
	getpdfCmd.PersistentFlags().StringVar(&getPdfData.RequestFile, "requestidfile", "", "File containing the request id (»Seriennummer«) on the first line")
	getpdfCmd.MarkFlagFilename("requestidfile", "")
	getpdfCmd.PersistentFlags().StringVarP(&cmdArguments.Outdir, "outfile", "o", "{{- .RequestID }}.pdf", "Directory for all output files")
	getpdfCmd.PersistentFlags().StringVar(&cmdArguments.PIN, "pin", "", "Request PIN")
	getpdfCmd.MarkFlagRequired("pin")
}