...

Source file src/github.com/sassoftware/relic/cmdline/remotecmd/getkeycmd.go

Documentation: github.com/sassoftware/relic/cmdline/remotecmd

     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  	"encoding/json"
    21  	"errors"
    22  	"io/ioutil"
    23  	"net/url"
    24  	"os"
    25  
    26  	"github.com/sassoftware/relic/cmdline/shared"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var GetKeyCmd = &cobra.Command{
    31  	Use:   "get-key",
    32  	Short: "Get a public key certificate from the remote server",
    33  	RunE:  getKeyCmd,
    34  }
    35  
    36  func init() {
    37  	RemoteCmd.AddCommand(GetKeyCmd)
    38  }
    39  
    40  type keyInfo struct {
    41  	X509Certificate string
    42  	PGPCertificate  string
    43  }
    44  
    45  func getKeyInfo(keyName string) (keyInfo, error) {
    46  	response, err := CallRemote("keys/"+url.PathEscape(keyName), "GET", nil, nil)
    47  	if err != nil {
    48  		return keyInfo{}, err
    49  	}
    50  	blob, err := ioutil.ReadAll(response.Body)
    51  	if err != nil {
    52  		return keyInfo{}, err
    53  	}
    54  	response.Body.Close()
    55  	var info keyInfo
    56  	if err := json.Unmarshal(blob, &info); err != nil {
    57  		return keyInfo{}, err
    58  	}
    59  	return info, nil
    60  }
    61  
    62  func getKeyCmd(cmd *cobra.Command, args []string) error {
    63  	if len(args) == 0 {
    64  		return errors.New("specify one or more key names. See also 'list-keys'")
    65  	}
    66  	for _, keyName := range args {
    67  		info, err := getKeyInfo(keyName)
    68  		if err != nil {
    69  			return shared.Fail(err)
    70  		}
    71  		os.Stdout.WriteString(info.X509Certificate)
    72  		os.Stdout.WriteString(info.PGPCertificate)
    73  	}
    74  	return nil
    75  }
    76  

View as plain text