Skip to content

Commit

Permalink
Allow non-cgo builds to use filetoken and scdtoken
Browse files Browse the repository at this point in the history
  • Loading branch information
mtharp committed Apr 20, 2018
1 parent a039b7a commit e0b719a
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 46 deletions.
25 changes: 13 additions & 12 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions cmdline/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package token

import (
"errors"
"fmt"
"os"
"sort"
"strings"

"github.com/sassoftware/relic/cmdline/shared"
"github.com/sassoftware/relic/config"
"github.com/sassoftware/relic/token"
"github.com/sassoftware/relic/token/p11token"
"github.com/sassoftware/relic/token/scdtoken"
"github.com/sassoftware/relic/token/open"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -55,7 +57,6 @@ var (
func init() {
shared.RootCmd.AddCommand(TokenCmd)
TokenCmd.PersistentFlags().StringVarP(&argToken, "token", "t", "", "Name of token")
TokenCmd.PersistentFlags().StringVar(&argType, "type", "", "Provider type (pkcs11, scdaemon)")
TokenCmd.PersistentFlags().StringVar(&argProvider, "provider", "", "Provider module path")

TokenCmd.AddCommand(TokensCmd)
Expand All @@ -64,6 +65,17 @@ func init() {
ContentsCmd.Flags().StringVarP(&argLabel, "label", "l", "", "Display objects with this label only")
ContentsCmd.Flags().StringVarP(&argId, "id", "i", "", "Display objects with this ID only")
ContentsCmd.Flags().BoolVarP(&argValues, "values", "v", false, "Show contents of objects")

shared.AddLateHook(addProviderTypeHelp) // deferred so token providers can init()
}

func addProviderTypeHelp() {
var listable []string
for ptype := range token.Listers {
listable = append(listable, ptype)
}
sort.Strings(listable)
TokenCmd.PersistentFlags().StringVar(&argType, "type", "", fmt.Sprintf("Provider type (%s)", strings.Join(listable, ", ")))
}

func tokensCmd(cmd *cobra.Command, args []string) error {
Expand All @@ -85,16 +97,7 @@ func tokensCmd(cmd *cobra.Command, args []string) error {
argProvider = tokenConf.Provider
}
}
var err error
switch argType {
case "pkcs11":
err = p11token.List(argProvider, os.Stdout)
case "scdaemon":
err = scdtoken.List(argProvider, os.Stdout)
default:
return errors.New("unsupported provider type")
}
return shared.Fail(err)
return shared.Fail(open.List(argType, argProvider, os.Stdout))
}

func contentsCmd(cmd *cobra.Command, args []string) error {
Expand Down
6 changes: 1 addition & 5 deletions impure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@

package main

// Local signing commands using tokens

import (
_ "github.com/sassoftware/relic/cmdline/token"
)
import _ "github.com/sassoftware/relic/token/p11token"
8 changes: 6 additions & 2 deletions nonclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

package main

// Commands that are disabled for client-only builds

import (
// Commands that are disabled for client-only builds
_ "github.com/sassoftware/relic/cmdline/auditor"
_ "github.com/sassoftware/relic/cmdline/servecmd"
_ "github.com/sassoftware/relic/cmdline/token"

// Token types that don't require cgo
_ "github.com/sassoftware/relic/token/filetoken"
_ "github.com/sassoftware/relic/token/scdtoken"
)
4 changes: 4 additions & 0 deletions token/filetoken/filetoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (
"github.com/sassoftware/relic/token"
)

func init() {
token.Openers["file"] = Open
}

type fileToken struct {
config *config.Config
tokenConf *config.TokenConfig
Expand Down
34 changes: 22 additions & 12 deletions token/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,26 @@ package open

import (
"fmt"
"io"

"github.com/sassoftware/relic/config"
"github.com/sassoftware/relic/lib/passprompt"
"github.com/sassoftware/relic/token"
"github.com/sassoftware/relic/token/filetoken"
"github.com/sassoftware/relic/token/p11token"
"github.com/sassoftware/relic/token/scdtoken"
)

func Token(cfg *config.Config, tokenName string, prompt passprompt.PasswordGetter) (token.Token, error) {
tcfg, err := cfg.GetToken(tokenName)
if err != nil {
return nil, err
}
switch tcfg.Type {
case "pkcs11":
return p11token.Open(cfg, tokenName, prompt)
case "file":
return filetoken.Open(cfg, tokenName, prompt)
case "scdaemon":
return scdtoken.Open(cfg, tokenName, prompt)
default:
return nil, fmt.Errorf("unknown token type %s", tcfg.Type)
if ofunc := token.Openers[tcfg.Type]; ofunc != nil {
return ofunc(cfg, tokenName, prompt)
}
var msg string
if tcfg.Type == "pkcs11" {
msg = " -- built without pkcs11 support"
}
return nil, fmt.Errorf("unknown token type %s%s", tcfg.Type, msg)
}

func Key(cfg *config.Config, keyName string, prompt passprompt.PasswordGetter) (token.Key, error) {
Expand All @@ -60,3 +56,17 @@ func Key(cfg *config.Config, keyName string, prompt passprompt.PasswordGetter) (
}
return key, nil
}

func List(tokenType, provider string, w io.Writer) error {
if listFunc := token.Listers[tokenType]; listFunc != nil {
return listFunc(provider, w)
}
if token.Openers[tokenType] != nil {
return fmt.Errorf("list operation not supported for token type %s", tokenType)
}
var msg string
if tokenType == "pkcs11" {
msg = " -- built without pkcs11 support"
}
return fmt.Errorf("unknown token type %s%s", tokenType, msg)
}
10 changes: 10 additions & 0 deletions token/p11token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const (
CKK_ECDSA = pkcs11.CKK_ECDSA
)

func init() {
token.Openers["pkcs11"] = open
token.Listers["pkcs11"] = List
}

var providerMap map[string]*pkcs11.Ctx
var providerMutex sync.Mutex

Expand Down Expand Up @@ -115,6 +120,11 @@ func Open(config *config.Config, tokenName string, pinProvider passprompt.Passwo
return tok, nil
}

// compat shim for token.Openers
func open(cfg *config.Config, tokenName string, prompt passprompt.PasswordGetter) (token.Token, error) {
return Open(cfg, tokenName, prompt)
}

func openLib(tokenConf *config.TokenConfig, write bool) (*pkcs11.Ctx, error) {
if tokenConf.Provider == "" {
return nil, errors.New("Missing attribute \"provider\" in token configuration")
Expand Down
34 changes: 34 additions & 0 deletions token/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright (c) SAS Institute Inc.
//
// 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 token

import (
"io"

"github.com/sassoftware/relic/config"
"github.com/sassoftware/relic/lib/passprompt"
)

type (
OpenFunc func(cfg *config.Config, tokenName string, prompt passprompt.PasswordGetter) (Token, error)
ListFunc func(provider string, dest io.Writer) error
)

var (
Openers = make(map[string]OpenFunc)
Listers = make(map[string]ListFunc)
)
9 changes: 7 additions & 2 deletions token/scdtoken/scdtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import (
"github.com/sassoftware/relic/token"
)

func init() {
token.Openers["scdtoken"] = Open
token.Listers["scdtoken"] = List
}

var defaultScdSockets = []string{
"/run/user/$UID/gnupg/S.scdaemon",
"/var/run/user/$UID/gnupg/S.scdaemon",
Expand Down Expand Up @@ -95,7 +100,7 @@ func List(sockPath string, output io.Writer) error {
return nil
}

func Open(conf *config.Config, tokenName string, prompt passprompt.PasswordGetter) (tok *scdToken, err error) {
func Open(conf *config.Config, tokenName string, prompt passprompt.PasswordGetter) (token.Token, error) {
tconf, err := conf.GetToken(tokenName)
if err != nil {
return nil, err
Expand All @@ -111,7 +116,7 @@ func Open(conf *config.Config, tokenName string, prompt passprompt.PasswordGette
if err != nil {
return nil, err
}
tok = &scdToken{
tok := &scdToken{
config: conf,
tokenConf: tconf,
sock: sock,
Expand Down

0 comments on commit e0b719a

Please sign in to comment.