2014-02-24 14:48:14 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-06-27 18:54:39 -04:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
2014-02-24 14:48:14 -05:00
|
|
|
"fmt"
|
2016-06-27 18:54:39 -04:00
|
|
|
"os"
|
2015-01-23 17:44:30 -05:00
|
|
|
"path/filepath"
|
2014-05-12 18:26:23 -04:00
|
|
|
|
2016-06-27 18:54:39 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-04-23 18:55:36 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-12-03 20:36:14 -05:00
|
|
|
"github.com/docker/libtrust"
|
2014-02-24 14:48:14 -05:00
|
|
|
)
|
|
|
|
|
2015-03-23 21:30:09 -04:00
|
|
|
// Common constants for daemon and client.
|
2014-02-24 14:48:14 -05:00
|
|
|
const (
|
2016-10-03 14:49:49 -04:00
|
|
|
// DefaultVersion of Current REST API
|
2017-06-09 08:39:55 -04:00
|
|
|
DefaultVersion string = "1.31"
|
2015-06-17 17:57:32 -04:00
|
|
|
|
2015-12-31 08:57:58 -05:00
|
|
|
// NoBaseImageSpecifier is the symbol used by the FROM
|
|
|
|
// command to specify that no base image is to be used.
|
|
|
|
NoBaseImageSpecifier string = "scratch"
|
2014-02-24 14:48:14 -05:00
|
|
|
)
|
|
|
|
|
2014-11-17 14:23:41 -05:00
|
|
|
// LoadOrCreateTrustKey attempts to load the libtrust key at the given path,
|
|
|
|
// otherwise generates a new one
|
|
|
|
func LoadOrCreateTrustKey(trustKeyPath string) (libtrust.PrivateKey, error) {
|
2017-06-01 21:59:11 -04:00
|
|
|
err := system.MkdirAll(filepath.Dir(trustKeyPath), 0700, "")
|
2015-04-23 18:55:36 -04:00
|
|
|
if err != nil {
|
2014-11-17 14:23:41 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trustKey, err := libtrust.LoadKeyFile(trustKeyPath)
|
|
|
|
if err == libtrust.ErrKeyFileDoesNotExist {
|
|
|
|
trustKey, err = libtrust.GenerateECP256PrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error generating key: %s", err)
|
|
|
|
}
|
2016-06-27 18:54:39 -04:00
|
|
|
encodedKey, err := serializePrivateKey(trustKey, filepath.Ext(trustKeyPath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error serializing key: %s", err)
|
|
|
|
}
|
|
|
|
if err := ioutils.AtomicWriteFile(trustKeyPath, encodedKey, os.FileMode(0600)); err != nil {
|
2014-11-17 14:23:41 -05:00
|
|
|
return nil, fmt.Errorf("Error saving key file: %s", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2015-01-26 15:58:45 -05:00
|
|
|
return nil, fmt.Errorf("Error loading key file %s: %s", trustKeyPath, err)
|
2014-11-17 14:23:41 -05:00
|
|
|
}
|
|
|
|
return trustKey, nil
|
|
|
|
}
|
2016-06-27 18:54:39 -04:00
|
|
|
|
|
|
|
func serializePrivateKey(key libtrust.PrivateKey, ext string) (encoded []byte, err error) {
|
|
|
|
if ext == ".json" || ext == ".jwk" {
|
|
|
|
encoded, err = json.Marshal(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to encode private key JWK: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pemBlock, err := key.PEMBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to encode private key PEM: %s", err)
|
|
|
|
}
|
|
|
|
encoded = pem.EncodeToMemory(pemBlock)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|