2014-10-01 21:26:06 -04:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-01 21:26:06 -04:00
|
|
|
"github.com/docker/libtrust"
|
|
|
|
)
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// NotVerifiedError reports a error when doing the key check.
|
|
|
|
// For example if the graph is not verified or the key has expired.
|
2015-04-20 15:48:33 -04:00
|
|
|
type NotVerifiedError string
|
2014-10-01 21:26:06 -04:00
|
|
|
|
2015-04-20 15:48:33 -04:00
|
|
|
func (e NotVerifiedError) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
2014-10-01 21:26:06 -04:00
|
|
|
|
2015-07-28 14:18:04 -04:00
|
|
|
// CheckKey verifies that the given public key is allowed to perform
|
|
|
|
// the given action on the given node according to the trust graph.
|
|
|
|
func (t *Store) CheckKey(ns string, key []byte, perm uint16) (bool, error) {
|
2015-04-20 15:48:33 -04:00
|
|
|
if len(key) == 0 {
|
|
|
|
return false, fmt.Errorf("Missing PublicKey")
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
2015-04-20 15:48:33 -04:00
|
|
|
pk, err := libtrust.UnmarshalPublicKeyJWK(key)
|
2014-10-01 21:26:06 -04:00
|
|
|
if err != nil {
|
2015-04-20 15:48:33 -04:00
|
|
|
return false, fmt.Errorf("Error unmarshalling public key: %v", err)
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
|
|
|
|
2015-04-20 15:48:33 -04:00
|
|
|
if perm == 0 {
|
|
|
|
perm = 0x03
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
if t.graph == nil {
|
2015-04-20 15:48:33 -04:00
|
|
|
return false, NotVerifiedError("no graph")
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any expired grants
|
2015-04-20 15:48:33 -04:00
|
|
|
verified, err := t.graph.Verify(pk, ns, perm)
|
2014-10-01 21:26:06 -04:00
|
|
|
if err != nil {
|
2015-04-20 15:48:33 -04:00
|
|
|
return false, fmt.Errorf("Error verifying key to namespace: %s", ns)
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
|
|
|
if !verified {
|
2015-04-20 15:48:33 -04:00
|
|
|
logrus.Debugf("Verification failed for %s using key %s", ns, pk.KeyID())
|
|
|
|
return false, NotVerifiedError("not verified")
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
2015-04-20 15:48:33 -04:00
|
|
|
if t.expiration.Before(time.Now()) {
|
|
|
|
return false, NotVerifiedError("expired")
|
|
|
|
}
|
|
|
|
return true, nil
|
2014-10-01 21:26:06 -04:00
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// UpdateBase retrieves updated base graphs. This function cannot error, it
|
|
|
|
// should only log errors.
|
2015-07-28 14:18:04 -04:00
|
|
|
func (t *Store) UpdateBase() {
|
2014-10-01 21:26:06 -04:00
|
|
|
t.fetch()
|
|
|
|
}
|