Skip to content

Commit

Permalink
Don't write temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Ploughman committed Jul 23, 2018
1 parent 7e47150 commit b0bc1f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
**/*.sh
**/data
**/data/*.json
**/test
**/test
**/releases/*
4 changes: 0 additions & 4 deletions data/claim.json

This file was deleted.

12 changes: 11 additions & 1 deletion path_trustee.go
Expand Up @@ -15,6 +15,7 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -25,6 +26,7 @@ import (
"time"

jwt "github.com/dgrijalva/jwt-go"

"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -364,7 +366,7 @@ func (b *backend) pathCreateJWT(ctx context.Context, req *logical.Request, data
}
claims["exp"] = fmt.Sprintf("%d", timeStart.Add(timeExpiry).Unix())

key, err := b.getTrusteePrivateKey(prunedPath, *trustee)
key, err := b.getTrusteePrivateKey(*trustee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -450,3 +452,11 @@ func (b *backend) verifyClaim(ctx context.Context, rawToken string) (jwt.MapClai
}
return nil, fmt.Errorf("Error verifying token")
}

// PrettyPrint prints an indented JSON payload. This is used for development debugging.
func PrettyPrint(v interface{}) string {
jsonString, _ := json.Marshal(v)
var out bytes.Buffer
json.Indent(&out, jsonString, "", " ")
return out.String()
}
20 changes: 6 additions & 14 deletions util.go
Expand Up @@ -157,6 +157,7 @@ func (b *backend) readJSONKeystore(keystorePath string) ([]byte, error) {
var jsonKeystore []byte
file, err := os.Open(keystorePath)
defer file.Close()
defer b.removeTemporaryKeystore(keystorePath)
stat, err := file.Stat()
if err != nil {
return nil, err
Expand All @@ -174,22 +175,13 @@ func (b *backend) readJSONKeystore(keystorePath string) ([]byte, error) {

}

func (b *backend) getTrusteePrivateKey(path string, trustee Trustee) (*keystore.Key, error) {
tmpDir, err := b.createTemporaryKeystoreDirectory()
if err != nil {
return nil, err
}
func (b *backend) getTrusteePrivateKey(trustee Trustee) (*keystore.Key, error) {
key, _ := keystore.DecryptKey(trustee.JSONKeystore, trustee.Passphrase)

keystorePath, err := b.writeTemporaryKeystoreFile(tmpDir, trustee.KeystoreName, trustee.JSONKeystore)
if err != nil {
return nil, err
}
key, err := b.readKeyFromJSONKeystore(keystorePath, trustee.Passphrase)
if err != nil {
return nil, err
if key != nil && key.PrivateKey != nil {
return key, nil
}
err = b.removeTemporaryKeystore(tmpDir)
return key, err
return nil, fmt.Errorf("failed to read key from keystore")
}

func (b *backend) exportKeystore(path string, trustee *Trustee) (string, error) {
Expand Down

0 comments on commit b0bc1f0

Please sign in to comment.