Skip to content

Commit

Permalink
test against supported go versions (keybase#74)
Browse files Browse the repository at this point in the history
* test against supported go versions

* appease ci
  • Loading branch information
joshblum authored Nov 19, 2021
1 parent 5d323af commit e6e08d5
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ os:

before_install:
- go get golang.org/x/lint/golint
- go mod tidy

script:
- go vet ./...
- golint ./...
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.32.2
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.43.0
- golangci-lint run
- go test -tags skipsecretserviceintegrationtests ./...

go:
- 1.14.x
- 1.15.x
- 1.16.x
- 1.17.x
1 change: 1 addition & 0 deletions bind/bind.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin || ios
// +build darwin ios

package bind
Expand Down
1 change: 1 addition & 0 deletions bindtest/bind_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin || ios
// +build darwin ios

package bindtest
Expand Down
1 change: 1 addition & 0 deletions corefoundation.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin || ios
// +build darwin ios

package keychain
Expand Down
1 change: 1 addition & 0 deletions datetime.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin || ios
// +build darwin ios

package keychain
Expand Down
1 change: 1 addition & 0 deletions datetime_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin && !ios
// +build darwin,!ios

package keychain
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module github.com/keybase/go-keychain

go 1.14
go 1.17

require (
github.com/keybase/go.dbus v0.0.0-20200324223359-a94be52c0b03
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
1 change: 1 addition & 0 deletions ios.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin && ios
// +build darwin,ios

package keychain
Expand Down
2 changes: 1 addition & 1 deletion keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
ErrorDecode = Error(C.errSecDecode)
// ErrorNoSuchKeychain corresponds to errSecNoSuchKeychain result code
ErrorNoSuchKeychain = Error(C.errSecNoSuchKeychain)
// ErrorNoAcccessForItem corresponds to errSecNoAccessForItem result code
// ErrorNoAccessForItem corresponds to errSecNoAccessForItem result code
ErrorNoAccessForItem = Error(C.errSecNoAccessForItem)
// ErrorReadOnly corresponds to errSecReadOnly result code
ErrorReadOnly = Error(C.errSecReadOnly)
Expand Down
48 changes: 42 additions & 6 deletions secretservice/secretservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,64 @@ import (
errors "github.com/pkg/errors"
)

// SecretServiceInterface
const SecretServiceInterface = "org.freedesktop.secrets"

// SecretServiceObjectPath
const SecretServiceObjectPath dbus.ObjectPath = "/org/freedesktop/secrets"

// DefaultCollection need not necessarily exist in the user's keyring.
const DefaultCollection dbus.ObjectPath = "/org/freedesktop/secrets/aliases/default"

type authenticationMode string
// AuthenticationMode
type AuthenticationMode string

// AuthenticationInsecurePlain
const AuthenticationInsecurePlain AuthenticationMode = "plain"

const AuthenticationInsecurePlain authenticationMode = "plain"
const AuthenticationDHAES authenticationMode = "dh-ietf1024-sha256-aes128-cbc-pkcs7"
// AuthenticationDHAES
const AuthenticationDHAES AuthenticationMode = "dh-ietf1024-sha256-aes128-cbc-pkcs7"

// NilFlags
const NilFlags = 0

// Attributes
type Attributes map[string]string

// Secret
type Secret struct {
Session dbus.ObjectPath
Parameters []byte
Value []byte
ContentType string
}

// PromptCompletedResult
type PromptCompletedResult struct {
Dismissed bool
Paths dbus.Variant
}

// SecretService
type SecretService struct {
conn *dbus.Conn
signalCh <-chan *dbus.Signal
sessionOpenTimeout time.Duration
}

// Session
type Session struct {
Mode authenticationMode
Mode AuthenticationMode
Path dbus.ObjectPath
Public *big.Int
Private *big.Int
AESKey []byte
}

// DefaultSessionOpenTimeout
const DefaultSessionOpenTimeout = 10 * time.Second

// NewService
func NewService() (*SecretService, error) {
conn, err := dbus.SessionBus()
if err != nil {
Expand All @@ -61,14 +76,17 @@ func NewService() (*SecretService, error) {
return &SecretService{conn: conn, signalCh: signalCh, sessionOpenTimeout: DefaultSessionOpenTimeout}, nil
}

// SetSessionOpenTimeout
func (s *SecretService) SetSessionOpenTimeout(d time.Duration) {
s.sessionOpenTimeout = d
}

// ServiceObj
func (s *SecretService) ServiceObj() *dbus.Object {
return s.conn.Object(SecretServiceInterface, SecretServiceObjectPath)
}

// Obj
func (s *SecretService) Obj(path dbus.ObjectPath) *dbus.Object {
return s.conn.Object(SecretServiceInterface, path)
}
Expand All @@ -78,14 +96,15 @@ type sessionOpenResponse struct {
path dbus.ObjectPath
}

func (s *SecretService) openSessionRaw(mode authenticationMode, sessionAlgorithmInput dbus.Variant) (resp sessionOpenResponse, err error) {
func (s *SecretService) openSessionRaw(mode AuthenticationMode, sessionAlgorithmInput dbus.Variant) (resp sessionOpenResponse, err error) {
err = s.ServiceObj().
Call("org.freedesktop.Secret.Service.OpenSession", NilFlags, mode, sessionAlgorithmInput).
Store(&resp.algorithmOutput, &resp.path)
return resp, errors.Wrap(err, "failed to open secretservice session")
}

func (s *SecretService) OpenSession(mode authenticationMode) (session *Session, err error) {
// OpenSession
func (s *SecretService) OpenSession(mode AuthenticationMode) (session *Session, err error) {
var sessionAlgorithmInput dbus.Variant

session = new(Session)
Expand Down Expand Up @@ -157,10 +176,12 @@ func (s *SecretService) OpenSession(mode authenticationMode) (session *Session,
return session, nil
}

// CloseSession
func (s *SecretService) CloseSession(session *Session) {
s.Obj(session.Path).Call("org.freedesktop.Secret.Session.Close", NilFlags)
}

// SearchColleciton
func (s *SecretService) SearchCollection(collection dbus.ObjectPath, attributes Attributes) (items []dbus.ObjectPath, err error) {
err = s.Obj(collection).
Call("org.freedesktop.Secret.Collection.SearchItems", NilFlags, attributes).
Expand All @@ -171,11 +192,16 @@ func (s *SecretService) SearchCollection(collection dbus.ObjectPath, attributes
return items, nil
}

// ReplaceBehavior
type ReplaceBehavior int

// ReplaceBehaviorDoNotReplace
const ReplaceBehaviorDoNotReplace = 0

// ReplaceBehaviorReplace
const ReplaceBehaviorReplace = 1

// CreateItem
func (s *SecretService) CreateItem(collection dbus.ObjectPath, properties map[string]dbus.Variant, secret Secret, replaceBehavior ReplaceBehavior) (item dbus.ObjectPath, err error) {
var replace bool
switch replaceBehavior {
Expand All @@ -201,6 +227,7 @@ func (s *SecretService) CreateItem(collection dbus.ObjectPath, properties map[st
return item, nil
}

// DeleteItem
func (s *SecretService) DeleteItem(item dbus.ObjectPath) (err error) {
var prompt dbus.ObjectPath
err = s.Obj(item).
Expand All @@ -216,6 +243,7 @@ func (s *SecretService) DeleteItem(item dbus.ObjectPath) (err error) {
return nil
}

// GetAttributes
func (s *SecretService) GetAttributes(item dbus.ObjectPath) (attributes Attributes, err error) {
attributesV, err := s.Obj(item).GetProperty("org.freedesktop.Secret.Item.Attributes")
if err != nil {
Expand All @@ -228,6 +256,7 @@ func (s *SecretService) GetAttributes(item dbus.ObjectPath) (attributes Attribut
return Attributes(attributesMap), nil
}

// GetSecret
func (s *SecretService) GetSecret(item dbus.ObjectPath, session Session) (secretPlaintext []byte, err error) {
var secretI []interface{}
err = s.Obj(item).
Expand Down Expand Up @@ -258,8 +287,10 @@ func (s *SecretService) GetSecret(item dbus.ObjectPath, session Session) (secret
return secretPlaintext, nil
}

// NullPrompt
const NullPrompt = "/"

// Unlock
func (s *SecretService) Unlock(items []dbus.ObjectPath) (err error) {
var dummy []dbus.ObjectPath
var prompt dbus.ObjectPath
Expand All @@ -276,6 +307,7 @@ func (s *SecretService) Unlock(items []dbus.ObjectPath) (err error) {
return nil
}

// LockItems
func (s *SecretService) LockItems(items []dbus.ObjectPath) (err error) {
var dummy []dbus.ObjectPath
var prompt dbus.ObjectPath
Expand All @@ -292,10 +324,12 @@ func (s *SecretService) LockItems(items []dbus.ObjectPath) (err error) {
return nil
}

// PromptDismissedError
type PromptDismissedError struct {
err error
}

// Error
func (p PromptDismissedError) Error() string {
return p.err.Error()
}
Expand Down Expand Up @@ -336,13 +370,15 @@ func (s *SecretService) PromptAndWait(prompt dbus.ObjectPath) (paths *dbus.Varia
}
}

// NewSecretProperties
func NewSecretProperties(label string, attributes map[string]string) map[string]dbus.Variant {
return map[string]dbus.Variant{
"org.freedesktop.Secret.Item.Label": dbus.MakeVariant(label),
"org.freedesktop.Secret.Item.Attributes": dbus.MakeVariant(attributes),
}
}

// NewSecret
func (session *Session) NewSecret(secretBytes []byte) (Secret, error) {
switch session.Mode {
case AuthenticationInsecurePlain:
Expand Down
3 changes: 2 additions & 1 deletion secretservice/secretservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// keyring with a default collection created.
// It should prompt you for your keyring password twice.

//go:build !skipsecretserviceintegrationtests
// +build !skipsecretserviceintegrationtests

package secretservice
Expand All @@ -21,7 +22,7 @@ func TestKeyringDH(t *testing.T) {
testKeyring(t, AuthenticationDHAES)
}

func testKeyring(t *testing.T, mode authenticationMode) {
func testKeyring(t *testing.T, mode AuthenticationMode) {
srv, err := NewService()
require.NoError(t, err)
session, err := srv.OpenSession(AuthenticationDHAES)
Expand Down

0 comments on commit e6e08d5

Please sign in to comment.