2014-10-02 05:26:06 +04:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2015-03-27 01:22:04 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-02 05:26:06 +04:00
|
|
|
"github.com/docker/libtrust"
|
|
|
|
)
|
|
|
|
|
2015-04-20 22:48:33 +03:00
|
|
|
type NotVerifiedError string
|
2014-10-02 05:26:06 +04:00
|
|
|
|
2015-04-20 22:48:33 +03:00
|
|
|
func (e NotVerifiedError) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
2014-10-02 05:26:06 +04:00
|
|
|
|
2015-04-20 22:48:33 +03:00
|
|
|
func (t *TrustStore) CheckKey(ns string, key []byte, perm uint16) (bool, error) {
|
|
|
|
if len(key) == 0 {
|
|
|
|
return false, fmt.Errorf("Missing PublicKey")
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
2015-04-20 22:48:33 +03:00
|
|
|
pk, err := libtrust.UnmarshalPublicKeyJWK(key)
|
2014-10-02 05:26:06 +04:00
|
|
|
if err != nil {
|
2015-04-20 22:48:33 +03:00
|
|
|
return false, fmt.Errorf("Error unmarshalling public key: %v", err)
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
|
|
|
|
2015-04-20 22:48:33 +03:00
|
|
|
if perm == 0 {
|
|
|
|
perm = 0x03
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
if t.graph == nil {
|
2015-04-20 22:48:33 +03:00
|
|
|
return false, NotVerifiedError("no graph")
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any expired grants
|
2015-04-20 22:48:33 +03:00
|
|
|
verified, err := t.graph.Verify(pk, ns, perm)
|
2014-10-02 05:26:06 +04:00
|
|
|
if err != nil {
|
2015-04-20 22:48:33 +03:00
|
|
|
return false, fmt.Errorf("Error verifying key to namespace: %s", ns)
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
|
|
|
if !verified {
|
2015-04-20 22:48:33 +03:00
|
|
|
logrus.Debugf("Verification failed for %s using key %s", ns, pk.KeyID())
|
|
|
|
return false, NotVerifiedError("not verified")
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
2015-04-20 22:48:33 +03:00
|
|
|
if t.expiration.Before(time.Now()) {
|
|
|
|
return false, NotVerifiedError("expired")
|
|
|
|
}
|
|
|
|
return true, nil
|
2014-10-02 05:26:06 +04:00
|
|
|
}
|
|
|
|
|
2015-04-20 22:48:33 +03:00
|
|
|
func (t *TrustStore) UpdateBase() {
|
2014-10-02 05:26:06 +04:00
|
|
|
t.fetch()
|
|
|
|
}
|