diff options
Diffstat (limited to 'cmd/cashier/main.go')
-rw-r--r-- | cmd/cashier/main.go | 109 |
1 files changed, 5 insertions, 104 deletions
diff --git a/cmd/cashier/main.go b/cmd/cashier/main.go index 72355e3..4ceaa80 100644 --- a/cmd/cashier/main.go +++ b/cmd/cashier/main.go @@ -1,24 +1,17 @@ package main import ( - "bytes" - "crypto/tls" - "encoding/json" "fmt" - "io/ioutil" "log" "net" - "net/http" - "net/url" "os" "os/user" "path" "time" - "github.com/nsheridan/cashier/lib" + "github.com/nsheridan/cashier/cmd/cashier/client" "github.com/pkg/browser" "github.com/spf13/pflag" - "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/agent" ) @@ -31,102 +24,10 @@ var ( keytype = pflag.String("key_type", "rsa", "Type of private key to generate - rsa, ecdsa or ed25519") ) -func installCert(a agent.Agent, cert *ssh.Certificate, key key) error { - t := time.Unix(int64(cert.ValidBefore), 0) - lifetime := t.Sub(time.Now()).Seconds() - comment := fmt.Sprintf("%s [Expires %s]", cert.KeyId, t) - pubcert := agent.AddedKey{ - PrivateKey: key, - Certificate: cert, - Comment: comment, - LifetimeSecs: uint32(lifetime), - } - if err := a.Add(pubcert); err != nil { - return fmt.Errorf("error importing certificate: %s", err) - } - privkey := agent.AddedKey{ - PrivateKey: key, - Comment: comment, - LifetimeSecs: uint32(lifetime), - } - if err := a.Add(privkey); err != nil { - return fmt.Errorf("error importing key: %s", err) - } - return nil -} - -func send(s []byte, token, ca string, ValidateTLSCertificate bool) (*lib.SignResponse, error) { - transport := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: !ValidateTLSCertificate}, - } - client := &http.Client{Transport: transport} - u, err := url.Parse(ca) - if err != nil { - return nil, err - } - u.Path = path.Join(u.Path, "/sign") - req, err := http.NewRequest("POST", u.String(), bytes.NewReader(s)) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "application/json") - req.Header.Add("Accept", "application/json") - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) - resp, err := client.Do(req) - if err != nil { - return nil, err - } - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("Bad response from server: %s", resp.Status) - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - c := &lib.SignResponse{} - if err := json.Unmarshal(body, c); err != nil { - return nil, err - } - return c, nil -} - -func sign(pub ssh.PublicKey, token string, conf *config) (*ssh.Certificate, error) { - validity, err := time.ParseDuration(conf.Validity) - if err != nil { - return nil, err - } - marshaled := ssh.MarshalAuthorizedKey(pub) - marshaled = marshaled[:len(marshaled)-1] - s, err := json.Marshal(&lib.SignRequest{ - Key: string(marshaled), - ValidUntil: time.Now().Add(validity), - }) - if err != nil { - return nil, err - } - resp, err := send(s, token, conf.CA, conf.ValidateTLSCertificate) - if err != nil { - return nil, err - } - if resp.Status != "ok" { - return nil, fmt.Errorf("error: %s", resp.Response) - } - k, _, _, _, err := ssh.ParseAuthorizedKey([]byte(resp.Response)) - if err != nil { - return nil, err - } - cert, ok := k.(*ssh.Certificate) - if !ok { - return nil, fmt.Errorf("did not receive a certificate from server") - } - return cert, nil -} - func main() { pflag.Parse() - c, err := readConfig(*cfg) + c, err := client.ReadConfig(*cfg) if err != nil { log.Fatalf("Error parsing config file: %v\n", err) } @@ -135,7 +36,7 @@ func main() { fmt.Println("Error launching web browser. Go to the link in your web browser") } fmt.Println("Generating new key pair") - priv, pub, err := generateKey(c.Keytype, c.Keysize) + priv, pub, err := client.GenerateKey(c.Keytype, c.Keysize) if err != nil { log.Fatalln("Error generating key pair: ", err) } @@ -144,7 +45,7 @@ func main() { var token string fmt.Scanln(&token) - cert, err := sign(pub, token, c) + cert, err := client.Sign(pub, token, c) if err != nil { log.Fatalln(err) } @@ -154,7 +55,7 @@ func main() { } defer sock.Close() a := agent.NewClient(sock) - if err := installCert(a, cert, priv); err != nil { + if err := client.InstallCert(a, cert, priv); err != nil { log.Fatalln(err) } fmt.Println("Credentials added.") |