-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added seed import. Preparing for integration tests
- Loading branch information
Showing
5 changed files
with
242 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright © 2018 Immutability, LLC | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/nats-io/nkeys" | ||
|
||
"github.com/hashicorp/vault/logical" | ||
"github.com/hashicorp/vault/logical/framework" | ||
) | ||
|
||
const ( | ||
// TypeNkey is a file with the .nk extension | ||
TypeNkey string = ".nk" | ||
// TypeCreds is a file with the .creds extension | ||
TypeCreds string = ".creds" | ||
) | ||
|
||
func importPaths(b *backend) []*framework.Path { | ||
return []*framework.Path{ | ||
&framework.Path{ | ||
Pattern: "import/" + framework.GenericNameRegex("name"), | ||
HelpSynopsis: "Import an nkey from file.", | ||
HelpDescription: ` | ||
Reads an nkey seed from file. | ||
`, | ||
Fields: map[string]*framework.FieldSchema{ | ||
"name": &framework.FieldSchema{Type: framework.TypeString}, | ||
"path": &framework.FieldSchema{ | ||
Type: framework.TypeString, | ||
Description: "Absolute path to the keystore file - not the parent directory.", | ||
}, | ||
}, | ||
ExistenceCheck: b.pathExistenceCheck, | ||
Callbacks: map[logical.Operation]framework.OperationFunc{ | ||
logical.CreateOperation: b.pathImportCreate, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (b *backend) pathImportCreate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { | ||
_, err := b.configured(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
name := data.Get("name").(string) | ||
var identity *Identity | ||
identity, err = b.readIdentity(ctx, req, name) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error reading identity") | ||
} | ||
if identity == nil { | ||
keystorePath := data.Get("path").(string) | ||
fileType := filepath.Ext(keystorePath) | ||
switch fileType { | ||
case TypeNkey: | ||
seed, err := ioutil.ReadFile(keystorePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pair, err := nkeys.FromSeed(seed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
identity, err = b.storeIdentity(ctx, req, name, pair, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = b.crossReference(ctx, req, name, identity.PublicKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
case TypeCreds: | ||
_, seed, err := credsFromNkeyFile(keystorePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pair, err := nkeys.FromSeed([]byte(seed)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
identity, err = b.storeIdentity(ctx, req, name, pair, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = b.crossReference(ctx, req, name, identity.PublicKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
default: | ||
return nil, fmt.Errorf("unknown file type") | ||
} | ||
return &logical.Response{ | ||
Data: map[string]interface{}{ | ||
"type": nkeys.Prefix(identity.PublicKey).String(), | ||
"trusted_keys": identity.TrustedKeys, | ||
"public_key": identity.PublicKey, | ||
}, | ||
}, nil | ||
} | ||
return nil, fmt.Errorf("account %s exists", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,33 @@ | |
[ "$trusted_keys" = "$account_key" ] | ||
} | ||
|
||
@test "import ngs account" { | ||
path=$HOME"/.nkeys/synadia/accounts/ngs/ngs.nk" | ||
user="$(vault write -format=json nkey/import/ngs-account path=$path | jq .data)" | ||
type="$(echo $user | jq -r .type)" | ||
[ "$type" = "account" ] | ||
} | ||
|
||
@test "import ngs user" { | ||
path=$HOME"/.nkeys/synadia/accounts/ngs/users/ngs.nk" | ||
account_key="$(vault read -format=json nkey/identities/ngs-account | jq -r .data.public_key)" | ||
user="$(vault write -format=json nkey/import/ngs-user path=$path | jq .data)" | ||
user_update="$(vault write -format=json nkey/identities/ngs-user trusted_keys=$account_key | jq .data)" | ||
type="$(echo $user | jq -r .type)" | ||
[ "$type" = "user" ] | ||
} | ||
|
||
@test "create ngs user claim" { | ||
issuer="$(vault read -format=json nkey/identities/ngs-account | jq -r .data.public_key)" | ||
subject="$(vault read -format=json nkey/identities/ngs-user | jq -r .data.public_key)" | ||
token="$(vault write -format=json nkey/identities/ngs-account/sign-claim subject=$subject type="user" [email protected] | jq -r .data.token)" | ||
response="$(vault write -format=json nkey/identities/ngs-user/verify-claim token=$token | jq .data)" | ||
response_issuer="$(echo $response | jq -r .issuer)" | ||
response_subject="$(echo $response | jq -r .public_key)" | ||
[ "$issuer" = "$response_issuer" ] | ||
[ "$subject" = "$response_subject" ] | ||
} | ||
|
||
@test "create account claim" { | ||
issuer="$(vault read -format=json nkey/identities/operator | jq -r .data.public_key)" | ||
subject="$(vault read -format=json nkey/identities/account | jq -r .data.public_key)" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters