func (d *Deleter) enter() (string, error) { dstCred := auth.NewBasicAuthCredential(d.dstUsr, d.dstPwd) dstClient, err := newRepositoryClient(d.dstURL, d.insecure, dstCred, d.repository, "repository", d.repository, "pull", "push", "*") if err != nil { d.logger.Errorf("an error occurred while creating destination repository client: %v", err) return "", err } d.dstClient = dstClient if len(d.tags) == 0 { tags, err := d.dstClient.ListTag() if err != nil { d.logger.Errorf("an error occurred while listing tags of repository %s on %s with user %s: %v", d.repository, d.dstURL, d.dstUsr, err) return "", err } d.tags = append(d.tags, tags...) } d.logger.Infof("tags %v will be deleted", d.tags) for _, tag := range d.tags { if err := d.dstClient.DeleteTag(tag); err != nil { d.logger.Errorf("an error occurred while deleting repository %s:%s on %s with user %s: %v", d.repository, tag, d.dstURL, d.dstUsr, err) return "", err } d.logger.Infof("repository %s:%s on %s has been deleted", d.repository, tag, d.dstURL) } return models.JobFinished, nil }
func (i *Initializer) enter() (string, error) { c := &http.Cookie{Name: models.UISecretCookie, Value: i.srcSecret} srcCred := auth.NewCookieCredential(c) srcClient, err := newRepositoryClient(i.srcURL, i.insecure, srcCred, i.repository, "repository", i.repository, "pull", "push", "*") if err != nil { i.logger.Errorf("an error occurred while creating source repository client: %v", err) return "", err } i.srcClient = srcClient dstCred := auth.NewBasicAuthCredential(i.dstUsr, i.dstPwd) dstClient, err := newRepositoryClient(i.dstURL, i.insecure, dstCred, i.repository, "repository", i.repository, "pull", "push", "*") if err != nil { i.logger.Errorf("an error occurred while creating destination repository client: %v", err) return "", err } i.dstClient = dstClient if len(i.tags) == 0 { tags, err := i.srcClient.ListTag() if err != nil { i.logger.Errorf("an error occurred while listing tags for source repository: %v", err) return "", err } i.tags = tags } i.logger.Infof("initialization completed: project: %s, repository: %s, tags: %v, source URL: %s, destination URL: %s, insecure: %v, destination user: %s", i.project, i.repository, i.tags, i.srcURL, i.dstURL, i.insecure, i.dstUsr) return StateCheck, nil }
func TestMain(m *testing.M) { //log.SetLevel(log.DebugLevel) credential = auth.NewBasicAuthCredential(username, password) tokenServer = initTokenServer() defer tokenServer.Close() registryServer = initRegistryServer() defer registryServer.Close() os.Exit(m.Run()) }
func newRegistryClient(endpoint string, insecure bool, username, password, scopeType, scopeName string, scopeActions ...string) (*registry.Registry, error) { credential := auth.NewBasicAuthCredential(username, password) authorizer := auth.NewStandardTokenAuthorizer(credential, insecure, scopeType, scopeName, scopeActions...) store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer) if err != nil { return nil, err } client, err := registry.NewRegistryWithModifiers(endpoint, insecure, store) if err != nil { return nil, err } return client, nil }
func TestListTagWithInvalidCredential(t *testing.T) { credential := auth.NewBasicAuthCredential(username, "wrong_password") client, err := newRepositoryClient(registryServer.URL, true, credential, repo, "repository", repo, "pull", "push", "*") if err != nil { t.Error(err) } if _, err = client.ListTag(); err != nil { e, ok := err.(*registry_error.Error) if ok && e.StatusCode == http.StatusUnauthorized { return } t.Error(err) return } }