Debian Codegraph @

cmdline/remotecmd/signcmd.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package remotecmd
18
19 import (
20 "errors"
21 "fmt"
22 "net/url"
23 "os"
24 "path/filepath"
25
26 "github.com/spf13/cobra"
27
28 "github.com/sassoftware/relic/v7/cmdline/shared"
29 "github.com/sassoftware/relic/v7/signers"
30 )
31
32 var SignCmd = &cobra.Command{
33 Use: "sign",
34 Short: "Sign a package using a remote signing server",
35 RunE: signCmd,
36 }
37
38 var (
39 argIfUnsigned bool
40 argSigType string
41 )
42
43 func init() {
44 RemoteCmd.AddCommand(SignCmd)
45 SignCmd.Flags().StringVarP(&argKeyName, "key", "k", "", "Name of key on remote server to use")
46 SignCmd.Flags().StringVarP(&argFile, "file", "f", "", "Input file to sign")
47 SignCmd.Flags().StringVarP(&argOutput, "output", "o", "", "Output file. Defaults to same as --file.")
48 SignCmd.Flags().StringVarP(&argSigType, "sig-type", "T", "", "Specify signature type (default: auto-detect)")
49 SignCmd.Flags().BoolVar(&argIfUnsigned, "if-unsigned", false, "Skip signing if the file already has a signature")
50 shared.AddDigestFlag(SignCmd)
51 shared.AddLateHook(func() {
52 signers.MergeFlags(SignCmd)
53 })
54 }
55
56 func signCmd(cmd *cobra.Command, args []string) (err error) {
57 if argFile == "" || argKeyName == "" {
58 return errors.New("--file and --key are required")
59 }
60 if argOutput == "" {
61 argOutput = argFile
62 }
63 // detect signature type
64 mod, err := signers.ByFile(argFile, argSigType)
65 if err != nil {
66 return shared.Fail(err)
67 }
68 if mod.Sign == nil {
69 return shared.Fail(fmt.Errorf("can't sign files of type: %s", mod.Name))
70 }
71 // parse signer-specific flags
72 flags, err := mod.FlagsFromCmdline(cmd.Flags())
73 if err != nil {
74 return shared.Fail(err)
75 }
76 infile, err := shared.OpenForPatching(argFile, argOutput)
77 if err != nil {
78 return shared.Fail(err)
79 } else if infile == os.Stdin {
80 if !mod.AllowStdin {
81 return shared.Fail(errors.New("this signature type does not support reading from stdin"))
82 }
83 } else {
84 defer infile.Close()
85 }
86 if argIfUnsigned {
87 if infile == os.Stdin {
88 return shared.Fail(errors.New("cannot use --if-unsigned with standard input"))
89 }
90 if signed, err := mod.IsSigned(infile); err != nil {
91 return shared.Fail(err)
92 } else if signed {
93 fmt.Fprintf(os.Stderr, "skipping already-signed file: %s\n", argFile)
94 return nil
95 }
96 if _, err := infile.Seek(0, 0); err != nil {
97 return shared.Fail(fmt.Errorf("rewinding input file: %w", err))
98 }
99 }
100 // transform input if needed
101 hash, err := shared.GetDigest()
102 if err != nil {
103 return err
104 }
105 opts := signers.SignOpts{
106 Path: argFile,
107 Hash: hash,
108 Flags: flags,
109 }
110 transform, err := mod.GetTransform(infile, opts)
111 if err != nil {
112 return shared.Fail(err)
113 }
114 // build request
115 values := url.Values{}
116 values.Add("key", argKeyName)
117 values.Add("filename", filepath.Base(argFile))
118 values.Add("sigtype", mod.Name)
119 if err := flags.ToQuery(values); err != nil {
120 return shared.Fail(err)
121 }
122 if err := setDigestQueryParam(values); err != nil {
123 return err
124 }
125 // do request
126 response, err := CallRemote("sign", "POST", &values, transform)
127 if err != nil {
128 return shared.Fail(err)
129 }
130 defer response.Body.Close()
131 // apply the result
132 if err := transform.Apply(argOutput, response.Header.Get("Content-Type"), response.Body); err != nil {
133 return shared.Fail(err)
134 }
135 // if needed, do a final fixup step
136 if mod.Fixup != nil {
137 f, err := os.OpenFile(argOutput, os.O_RDWR, 0)
138 if err != nil {
139 return shared.Fail(err)
140 }
141 defer f.Close()
142 if err := mod.Fixup(f); err != nil {
143 return shared.Fail(err)
144 }
145 }
146
147 fmt.Fprintf(os.Stderr, "Signed %s\n", argFile)
148 return nil
149 }