Forráskód Böngészése

Move authConfig from runtime to registry

Guillaume J. Charmes 12 éve
szülő
commit
95dd6d31a4
6 módosított fájl, 26 hozzáadás és 58 törlés
  1. 3 7
      api.go
  2. 7 4
      api_test.go
  3. 1 1
      graph.go
  4. 11 36
      registry/registry.go
  5. 1 8
      runtime.go
  6. 3 2
      server.go

+ 3 - 7
api.go

@@ -45,11 +45,7 @@ func writeJson(w http.ResponseWriter, b []byte) {
 }
 
 func getAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
-	config := &auth.AuthConfig{
-		Username: srv.runtime.authConfig.Username,
-		Email:    srv.runtime.authConfig.Email,
-	}
-	b, err := json.Marshal(config)
+	b, err := json.Marshal(srv.registry.GetAuthConfig())
 	if err != nil {
 		return err
 	}
@@ -63,8 +59,8 @@ func postAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[stri
 		return err
 	}
 
-	if config.Username == srv.runtime.authConfig.Username {
-		config.Password = srv.runtime.authConfig.Password
+	if config.Username == srv.registry.GetAuthConfig().Username {
+		config.Password = srv.registry.GetAuthConfig().Password
 	}
 
 	newAuthConfig := auth.NewAuthConfig(config.Username, config.Password, config.Email, srv.runtime.root)

+ 7 - 4
api_test.go

@@ -25,7 +25,10 @@ func TestGetAuth(t *testing.T) {
 	}
 	defer nuke(runtime)
 
-	srv := &Server{runtime: runtime}
+	srv := &Server{
+		runtime:  runtime,
+		registry: registry.NewRegistry(runtime.root),
+	}
 
 	r := httptest.NewRecorder()
 
@@ -52,9 +55,9 @@ func TestGetAuth(t *testing.T) {
 		t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
 	}
 
-	if runtime.authConfig.Username != authConfig.Username ||
-		runtime.authConfig.Password != authConfig.Password ||
-		runtime.authConfig.Email != authConfig.Email {
+	newAuthConfig := srv.registry.GetAuthConfig()
+	if newAuthConfig.Username != authConfig.Username ||
+		newAuthConfig.Email != authConfig.Email {
 		t.Fatalf("The auth configuration hasn't been set correctly")
 	}
 }

+ 1 - 1
graph.go

@@ -325,7 +325,7 @@ func (graph *Graph) storeChecksums(checksums map[string]string) error {
 	return nil
 }
 
-func (graph *Graph) UpdateChecksuns(newChecksums map[string]*registry.ImgData) error {
+func (graph *Graph) UpdateChecksums(newChecksums map[string]*registry.ImgData) error {
 	graph.lockSumFile.Lock()
 	defer graph.lockSumFile.Unlock()
 

+ 11 - 36
registry/registry.go

@@ -174,41 +174,6 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
 	return nil, fmt.Errorf("Could not reach any registry endpoint")
 }
 
-func (r *Registry) getImageForTag(tag, remote, registry string, token []string) (string, error) {
-	if !strings.Contains(remote, "/") {
-		remote = "library/" + remote
-	}
-
-	registryEndpoint := "https://" + registry + "/v1"
-	repositoryTarget := registryEndpoint + "/repositories/" + remote + "/tags/" + tag
-
-	req, err := http.NewRequest("GET", repositoryTarget, nil)
-	if err != nil {
-		return "", err
-	}
-	req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
-	res, err := r.client.Do(req)
-	if err != nil {
-		return "", fmt.Errorf("Error while retrieving repository info: %s", err)
-	}
-	defer res.Body.Close()
-	if res.StatusCode == 403 {
-		return "", fmt.Errorf("You aren't authorized to access this resource")
-	} else if res.StatusCode != 200 {
-		return "", fmt.Errorf("HTTP code: %d", res.StatusCode)
-	}
-
-	var imgId string
-	rawJson, err := ioutil.ReadAll(res.Body)
-	if err != nil {
-		return "", err
-	}
-	if err := json.Unmarshal(rawJson, &imgId); err != nil {
-		return "", err
-	}
-	return imgId, nil
-}
-
 func (r *Registry) GetRepositoryData(remote string) (*RepositoryData, error) {
 	utils.Debugf("Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress())
 	repositoryTarget := auth.IndexServerAddress() + "/repositories/" + remote + "/images"
@@ -464,6 +429,13 @@ func (r *Registry) ResetClient(authConfig *auth.AuthConfig) {
 	r.client.Jar = cookiejar.NewCookieJar()
 }
 
+func (r *Registry) GetAuthConfig() *auth.AuthConfig {
+	return &auth.AuthConfig{
+		Username: r.authConfig.Username,
+		Email:    r.authConfig.Password,
+	}
+}
+
 type SearchResults struct {
 	Query      string              `json:"query"`
 	NumResults int                 `json:"num_results"`
@@ -487,7 +459,10 @@ type Registry struct {
 	authConfig *auth.AuthConfig
 }
 
-func NewRegistry(authConfig *auth.AuthConfig) *Registry {
+func NewRegistry(root string) *Registry {
+	// If the auth file does not exist, keep going
+	authConfig, _ := auth.LoadConfig(root)
+
 	r := &Registry{
 		authConfig: authConfig,
 		client:     &http.Client{},

+ 1 - 8
runtime.go

@@ -3,7 +3,6 @@ package docker
 import (
 	"container/list"
 	"fmt"
-	"github.com/dotcloud/docker/auth"
 	"github.com/dotcloud/docker/utils"
 	"io"
 	"io/ioutil"
@@ -27,12 +26,12 @@ type Runtime struct {
 	networkManager *NetworkManager
 	graph          *Graph
 	repositories   *TagStore
-	authConfig     *auth.AuthConfig
 	idIndex        *utils.TruncIndex
 	capabilities   *Capabilities
 	kernelVersion  *utils.KernelVersionInfo
 	autoRestart    bool
 	volumes        *Graph
+	srv            *Server
 }
 
 var sysInitPath string
@@ -290,11 +289,6 @@ func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) {
 	if err != nil {
 		return nil, err
 	}
-	authConfig, err := auth.LoadConfig(root)
-	if err != nil && authConfig == nil {
-		// If the auth file does not exist, keep going
-		return nil, err
-	}
 	runtime := &Runtime{
 		root:           root,
 		repository:     runtimeRepo,
@@ -302,7 +296,6 @@ func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) {
 		networkManager: netManager,
 		graph:          g,
 		repositories:   repositories,
-		authConfig:     authConfig,
 		idIndex:        utils.NewTruncIndex(),
 		capabilities:   &Capabilities{},
 		autoRestart:    autoRestart,

+ 3 - 2
server.go

@@ -331,7 +331,7 @@ func (srv *Server) pullRepository(stdout io.Writer, remote, askedTag string) err
 
 	utils.Debugf("Updating checksums")
 	// Reload the json file to make sure not to overwrite faster sums
-	if err := srv.runtime.graph.UpdateChecksuns(repoData.ImgList); err != nil {
+	if err := srv.runtime.graph.UpdateChecksums(repoData.ImgList); err != nil {
 		return err
 	}
 
@@ -826,8 +826,9 @@ func NewServer(autoRestart bool) (*Server, error) {
 	}
 	srv := &Server{
 		runtime:  runtime,
-		registry: registry.NewRegistry(runtime.authConfig),
+		registry: registry.NewRegistry(runtime.root),
 	}
+	runtime.srv = srv
 	return srv, nil
 }