Browse code

Move authConfig from runtime to registry

Guillaume J. Charmes authored on 2013/05/16 09:17:33
Showing 6 changed files
... ...
@@ -45,11 +45,7 @@ func writeJson(w http.ResponseWriter, b []byte) {
45 45
 }
46 46
 
47 47
 func getAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
48
-	config := &auth.AuthConfig{
49
-		Username: srv.runtime.authConfig.Username,
50
-		Email:    srv.runtime.authConfig.Email,
51
-	}
52
-	b, err := json.Marshal(config)
48
+	b, err := json.Marshal(srv.registry.GetAuthConfig())
53 49
 	if err != nil {
54 50
 		return err
55 51
 	}
... ...
@@ -63,8 +59,8 @@ func postAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[stri
63 63
 		return err
64 64
 	}
65 65
 
66
-	if config.Username == srv.runtime.authConfig.Username {
67
-		config.Password = srv.runtime.authConfig.Password
66
+	if config.Username == srv.registry.GetAuthConfig().Username {
67
+		config.Password = srv.registry.GetAuthConfig().Password
68 68
 	}
69 69
 
70 70
 	newAuthConfig := auth.NewAuthConfig(config.Username, config.Password, config.Email, srv.runtime.root)
... ...
@@ -25,7 +25,10 @@ func TestGetAuth(t *testing.T) {
25 25
 	}
26 26
 	defer nuke(runtime)
27 27
 
28
-	srv := &Server{runtime: runtime}
28
+	srv := &Server{
29
+		runtime:  runtime,
30
+		registry: registry.NewRegistry(runtime.root),
31
+	}
29 32
 
30 33
 	r := httptest.NewRecorder()
31 34
 
... ...
@@ -52,9 +55,9 @@ func TestGetAuth(t *testing.T) {
52 52
 		t.Fatalf("%d OK or 0 expected, received %d\n", http.StatusOK, r.Code)
53 53
 	}
54 54
 
55
-	if runtime.authConfig.Username != authConfig.Username ||
56
-		runtime.authConfig.Password != authConfig.Password ||
57
-		runtime.authConfig.Email != authConfig.Email {
55
+	newAuthConfig := srv.registry.GetAuthConfig()
56
+	if newAuthConfig.Username != authConfig.Username ||
57
+		newAuthConfig.Email != authConfig.Email {
58 58
 		t.Fatalf("The auth configuration hasn't been set correctly")
59 59
 	}
60 60
 }
... ...
@@ -325,7 +325,7 @@ func (graph *Graph) storeChecksums(checksums map[string]string) error {
325 325
 	return nil
326 326
 }
327 327
 
328
-func (graph *Graph) UpdateChecksuns(newChecksums map[string]*registry.ImgData) error {
328
+func (graph *Graph) UpdateChecksums(newChecksums map[string]*registry.ImgData) error {
329 329
 	graph.lockSumFile.Lock()
330 330
 	defer graph.lockSumFile.Unlock()
331 331
 
... ...
@@ -174,41 +174,6 @@ func (r *Registry) GetRemoteTags(registries []string, repository string, token [
174 174
 	return nil, fmt.Errorf("Could not reach any registry endpoint")
175 175
 }
176 176
 
177
-func (r *Registry) getImageForTag(tag, remote, registry string, token []string) (string, error) {
178
-	if !strings.Contains(remote, "/") {
179
-		remote = "library/" + remote
180
-	}
181
-
182
-	registryEndpoint := "https://" + registry + "/v1"
183
-	repositoryTarget := registryEndpoint + "/repositories/" + remote + "/tags/" + tag
184
-
185
-	req, err := http.NewRequest("GET", repositoryTarget, nil)
186
-	if err != nil {
187
-		return "", err
188
-	}
189
-	req.Header.Set("Authorization", "Token "+strings.Join(token, ", "))
190
-	res, err := r.client.Do(req)
191
-	if err != nil {
192
-		return "", fmt.Errorf("Error while retrieving repository info: %s", err)
193
-	}
194
-	defer res.Body.Close()
195
-	if res.StatusCode == 403 {
196
-		return "", fmt.Errorf("You aren't authorized to access this resource")
197
-	} else if res.StatusCode != 200 {
198
-		return "", fmt.Errorf("HTTP code: %d", res.StatusCode)
199
-	}
200
-
201
-	var imgId string
202
-	rawJson, err := ioutil.ReadAll(res.Body)
203
-	if err != nil {
204
-		return "", err
205
-	}
206
-	if err := json.Unmarshal(rawJson, &imgId); err != nil {
207
-		return "", err
208
-	}
209
-	return imgId, nil
210
-}
211
-
212 177
 func (r *Registry) GetRepositoryData(remote string) (*RepositoryData, error) {
213 178
 	utils.Debugf("Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress())
214 179
 	repositoryTarget := auth.IndexServerAddress() + "/repositories/" + remote + "/images"
... ...
@@ -464,6 +429,13 @@ func (r *Registry) ResetClient(authConfig *auth.AuthConfig) {
464 464
 	r.client.Jar = cookiejar.NewCookieJar()
465 465
 }
466 466
 
467
+func (r *Registry) GetAuthConfig() *auth.AuthConfig {
468
+	return &auth.AuthConfig{
469
+		Username: r.authConfig.Username,
470
+		Email:    r.authConfig.Password,
471
+	}
472
+}
473
+
467 474
 type SearchResults struct {
468 475
 	Query      string              `json:"query"`
469 476
 	NumResults int                 `json:"num_results"`
... ...
@@ -487,7 +459,10 @@ type Registry struct {
487 487
 	authConfig *auth.AuthConfig
488 488
 }
489 489
 
490
-func NewRegistry(authConfig *auth.AuthConfig) *Registry {
490
+func NewRegistry(root string) *Registry {
491
+	// If the auth file does not exist, keep going
492
+	authConfig, _ := auth.LoadConfig(root)
493
+
491 494
 	r := &Registry{
492 495
 		authConfig: authConfig,
493 496
 		client:     &http.Client{},
... ...
@@ -3,7 +3,6 @@ package docker
3 3
 import (
4 4
 	"container/list"
5 5
 	"fmt"
6
-	"github.com/dotcloud/docker/auth"
7 6
 	"github.com/dotcloud/docker/utils"
8 7
 	"io"
9 8
 	"io/ioutil"
... ...
@@ -27,12 +26,12 @@ type Runtime struct {
27 27
 	networkManager *NetworkManager
28 28
 	graph          *Graph
29 29
 	repositories   *TagStore
30
-	authConfig     *auth.AuthConfig
31 30
 	idIndex        *utils.TruncIndex
32 31
 	capabilities   *Capabilities
33 32
 	kernelVersion  *utils.KernelVersionInfo
34 33
 	autoRestart    bool
35 34
 	volumes        *Graph
35
+	srv            *Server
36 36
 }
37 37
 
38 38
 var sysInitPath string
... ...
@@ -290,11 +289,6 @@ func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) {
290 290
 	if err != nil {
291 291
 		return nil, err
292 292
 	}
293
-	authConfig, err := auth.LoadConfig(root)
294
-	if err != nil && authConfig == nil {
295
-		// If the auth file does not exist, keep going
296
-		return nil, err
297
-	}
298 293
 	runtime := &Runtime{
299 294
 		root:           root,
300 295
 		repository:     runtimeRepo,
... ...
@@ -302,7 +296,6 @@ func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) {
302 302
 		networkManager: netManager,
303 303
 		graph:          g,
304 304
 		repositories:   repositories,
305
-		authConfig:     authConfig,
306 305
 		idIndex:        utils.NewTruncIndex(),
307 306
 		capabilities:   &Capabilities{},
308 307
 		autoRestart:    autoRestart,
... ...
@@ -331,7 +331,7 @@ func (srv *Server) pullRepository(stdout io.Writer, remote, askedTag string) err
331 331
 
332 332
 	utils.Debugf("Updating checksums")
333 333
 	// Reload the json file to make sure not to overwrite faster sums
334
-	if err := srv.runtime.graph.UpdateChecksuns(repoData.ImgList); err != nil {
334
+	if err := srv.runtime.graph.UpdateChecksums(repoData.ImgList); err != nil {
335 335
 		return err
336 336
 	}
337 337
 
... ...
@@ -826,8 +826,9 @@ func NewServer(autoRestart bool) (*Server, error) {
826 826
 	}
827 827
 	srv := &Server{
828 828
 		runtime:  runtime,
829
-		registry: registry.NewRegistry(runtime.authConfig),
829
+		registry: registry.NewRegistry(runtime.root),
830 830
 	}
831
+	runtime.srv = srv
831 832
 	return srv, nil
832 833
 }
833 834