Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 8edbeac

Browse files
borzamirceaehazlett
borzamircea
authored andcommitted
Feature/shipyard ready registry v2 (#852)
* Squash Cleaned up code for shipyard integration Cleanup ouf UI, .idea and gitignore Removed .idea again Fixed nonresponsive ui Removed whitespaces * Fixed mock manager missing method * Latest changes requested by Evan * display size for repository
1 parent da4501f commit 8edbeac

24 files changed

+901
-163
lines changed

Diff for: Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ image: media build
2727
release: build image
2828
@docker push shipyard/shipyard:$(TAG)
2929

30-
test: clean
30+
test: clean
3131
@godep go test -v ./...
3232

33-
.PHONY: all build clean media image test release
33+
.PHONY: all build clean media image test release

Diff for: auth/auth.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package auth
22

33
import (
44
"errors"
5+
"golang.org/x/crypto/bcrypt"
56
"strings"
67
"time"
7-
8-
"golang.org/x/crypto/bcrypt"
98
)
109

1110
var (

Diff for: auth/builtin/builtin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package builtin
22

33
import (
4-
"golang.org/x/crypto/bcrypt"
54
"github.com/shipyard/shipyard/auth"
5+
"golang.org/x/crypto/bcrypt"
66
)
77

88
type (

Diff for: auth/ldap/ldap.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package ldap
22

33
import (
44
"fmt"
5-
"strings"
65

76
log "github.com/Sirupsen/logrus"
87
"github.com/shipyard/shipyard/auth"
98
goldap "gopkg.in/ldap.v1"
9+
"strings"
1010
)
1111

1212
type (
@@ -45,17 +45,15 @@ func (a LdapAuthenticator) Authenticate(username, password, hash string) (bool,
4545
defer l.Close()
4646

4747
dn := fmt.Sprintf("cn=%s,%s", username, a.BaseDN)
48-
48+
if err := l.Bind(dn, password); err != nil {
49+
return false, err
50+
}
4951
if strings.Contains(a.BaseDN, "{username}") {
5052
dn = strings.Replace(a.BaseDN, "{username}", username, -1)
5153
}
5254

5355
log.Debugf("ldap authentication: dn=%s", dn)
5456

55-
if err := l.Bind(dn, password); err != nil {
56-
return false, err
57-
}
58-
5957
log.Debugf("ldap authentication successful: username=%s", username)
6058

6159
return true, nil

Diff for: controller/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
controller
2+
.idea/*

Diff for: controller/api/api.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ func (a *Api) Run() error {
128128
apiRouter.HandleFunc("/api/events", a.purgeEvents).Methods("DELETE")
129129
apiRouter.HandleFunc("/api/registries", a.registries).Methods("GET")
130130
apiRouter.HandleFunc("/api/registries", a.addRegistry).Methods("POST")
131-
apiRouter.HandleFunc("/api/registries/{name}", a.registry).Methods("GET")
132-
apiRouter.HandleFunc("/api/registries/{name}", a.removeRegistry).Methods("DELETE")
133-
apiRouter.HandleFunc("/api/registries/{name}/repositories", a.repositories).Methods("GET")
134-
apiRouter.HandleFunc("/api/registries/{name}/repositories/{repo:.*}", a.repository).Methods("GET")
135-
apiRouter.HandleFunc("/api/registries/{name}/repositories/{repo:.*}", a.deleteRepository).Methods("DELETE")
131+
apiRouter.HandleFunc("/api/registries/{registryId}", a.registry).Methods("GET")
132+
apiRouter.HandleFunc("/api/registries/{registryId}", a.removeRegistry).Methods("DELETE")
133+
apiRouter.HandleFunc("/api/registries/{registryId}/repositories", a.repositories).Methods("GET")
134+
apiRouter.HandleFunc("/api/registries/{registryId}/repositories/{repo:.*}", a.repository).Methods("GET")
135+
apiRouter.HandleFunc("/api/registries/{registryId}/repositories/{repo:.*}", a.deleteRepository).Methods("DELETE")
136136
apiRouter.HandleFunc("/api/servicekeys", a.serviceKeys).Methods("GET")
137137
apiRouter.HandleFunc("/api/servicekeys", a.addServiceKey).Methods("POST")
138138
apiRouter.HandleFunc("/api/servicekeys", a.removeServiceKey).Methods("DELETE")

Diff for: controller/api/registry.go

+28-48
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func (a *Api) registry(w http.ResponseWriter, r *http.Request) {
4343
w.Header().Set("content-type", "application/json")
4444

4545
vars := mux.Vars(r)
46-
name := vars["name"]
46+
id := vars["registryId"]
4747

48-
registry, err := a.manager.Registry(name)
48+
registry, err := a.manager.Registry(id)
4949
if err != nil {
5050
http.Error(w, err.Error(), http.StatusInternalServerError)
5151
return
@@ -59,9 +59,9 @@ func (a *Api) registry(w http.ResponseWriter, r *http.Request) {
5959

6060
func (a *Api) removeRegistry(w http.ResponseWriter, r *http.Request) {
6161
vars := mux.Vars(r)
62-
name := vars["name"]
62+
id := vars["registryId"]
6363

64-
registry, err := a.manager.Registry(name)
64+
registry, err := a.manager.Registry(id)
6565
if err != nil {
6666
http.Error(w, err.Error(), http.StatusInternalServerError)
6767
return
@@ -78,35 +78,37 @@ func (a *Api) repositories(w http.ResponseWriter, r *http.Request) {
7878
w.Header().Set("content-type", "application/json")
7979

8080
vars := mux.Vars(r)
81-
name := vars["name"]
82-
83-
if name != "" {
84-
registry, err := a.manager.Registry(name)
85-
if err != nil {
86-
http.Error(w, err.Error(), http.StatusInternalServerError)
87-
return
88-
}
89-
90-
repos, err := registry.Repositories()
91-
if err != nil {
92-
http.Error(w, err.Error(), http.StatusInternalServerError)
93-
return
94-
}
95-
if err := json.NewEncoder(w).Encode(repos); err != nil {
96-
http.Error(w, err.Error(), http.StatusInternalServerError)
97-
return
98-
}
81+
id := vars["registryId"]
82+
83+
if id == "" {
84+
http.Error(w, "Please pass a valid id", http.StatusNotFound)
85+
return
86+
}
87+
registry, err := a.manager.Registry(id)
88+
if err != nil {
89+
http.Error(w, err.Error(), http.StatusInternalServerError)
90+
return
91+
}
92+
93+
repos, err := registry.Repositories()
94+
if err != nil {
95+
http.Error(w, err.Error(), http.StatusInternalServerError)
96+
return
97+
}
98+
if err := json.NewEncoder(w).Encode(repos); err != nil {
99+
http.Error(w, err.Error(), http.StatusInternalServerError)
100+
return
99101
}
100102
}
101103

102104
func (a *Api) repository(w http.ResponseWriter, r *http.Request) {
103105
w.Header().Set("content-type", "application/json")
104106

105107
vars := mux.Vars(r)
106-
name := vars["name"]
108+
id := vars["registryId"]
107109
repoName := vars["repo"]
108110

109-
registry, err := a.manager.Registry(name)
111+
registry, err := a.manager.Registry(id)
110112
if err != nil {
111113
http.Error(w, err.Error(), http.StatusInternalServerError)
112114
return
@@ -125,10 +127,10 @@ func (a *Api) repository(w http.ResponseWriter, r *http.Request) {
125127

126128
func (a *Api) deleteRepository(w http.ResponseWriter, r *http.Request) {
127129
vars := mux.Vars(r)
128-
name := vars["name"]
130+
id := vars["registryId"]
129131
repoName := vars["repo"]
130132

131-
registry, err := a.manager.Registry(name)
133+
registry, err := a.manager.Registry(id)
132134
if err != nil {
133135
http.Error(w, err.Error(), http.StatusInternalServerError)
134136
return
@@ -141,25 +143,3 @@ func (a *Api) deleteRepository(w http.ResponseWriter, r *http.Request) {
141143

142144
w.WriteHeader(http.StatusNoContent)
143145
}
144-
145-
func (a *Api) inspectRepository(w http.ResponseWriter, r *http.Request) {
146-
vars := mux.Vars(r)
147-
name := vars["name"]
148-
repoName := vars["repo"]
149-
150-
registry, err := a.manager.Registry(name)
151-
if err != nil {
152-
http.Error(w, err.Error(), http.StatusInternalServerError)
153-
return
154-
}
155-
156-
repo, err := registry.Repository(repoName)
157-
if err != nil {
158-
http.Error(w, err.Error(), http.StatusInternalServerError)
159-
return
160-
}
161-
if err := json.NewEncoder(w).Encode(repo); err != nil {
162-
http.Error(w, err.Error(), http.StatusInternalServerError)
163-
return
164-
}
165-
}

Diff for: controller/manager/manager.go

+80-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"crypto/tls"
1314
log "github.com/Sirupsen/logrus"
1415
"github.com/gorilla/sessions"
1516
"github.com/samalba/dockerclient"
@@ -37,6 +38,7 @@ const (
3738
)
3839

3940
var (
41+
ErrCannotPingRegistry = errors.New("Cannot ping registry")
4042
ErrLoginFailure = errors.New("invalid username or password")
4143
ErrAccountExists = errors.New("account already exists")
4244
ErrAccountDoesNotExist = errors.New("account does not exist")
@@ -107,6 +109,7 @@ type (
107109
RemoveRegistry(registry *shipyard.Registry) error
108110
Registries() ([]*shipyard.Registry, error)
109111
Registry(name string) (*shipyard.Registry, error)
112+
RegistryByAddress(addr string) (*shipyard.Registry, error)
110113

111114
CreateConsoleSession(c *shipyard.ConsoleSession) error
112115
RemoveConsoleSession(c *shipyard.ConsoleSession) error
@@ -701,26 +704,67 @@ func (m DefaultManager) Node(name string) (*shipyard.Node, error) {
701704
return nil, nil
702705
}
703706

704-
func (m DefaultManager) AddRegistry(registry *shipyard.Registry) error {
705-
resp, err := http.Get(fmt.Sprintf("%s/v1/search", registry.Addr))
707+
func (m DefaultManager) PingRegistry(registry *shipyard.Registry) error {
708+
709+
// TODO: Please note the trailing forward slash / which is needed for Artifactory, else you get a 404.
710+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/", registry.Addr), nil)
711+
712+
if err != nil {
713+
return err
714+
}
715+
716+
req.SetBasicAuth(registry.Username, registry.Password)
717+
718+
var tlsConfig *tls.Config
719+
720+
tlsConfig = nil
721+
722+
if registry.TlsSkipVerify {
723+
tlsConfig = &tls.Config{InsecureSkipVerify: true}
724+
}
725+
726+
// Create unsecured client
727+
trans := &http.Transport{
728+
TLSClientConfig: tlsConfig,
729+
}
730+
731+
client := &http.Client{Transport: trans}
732+
733+
resp, err := client.Do(req)
734+
706735
if err != nil {
707736
return err
708737
}
709738
if resp.StatusCode != 200 {
710739
return errors.New(resp.Status)
711740
}
712741

713-
if _, err := r.Table(tblNameRegistries).Insert(registry).RunWrite(m.session); err != nil {
742+
return nil
743+
}
744+
745+
func (m DefaultManager) AddRegistry(registry *shipyard.Registry) error {
746+
747+
if err := registry.InitRegistryClient(); err != nil {
714748
return err
715749
}
716750

751+
// TODO: consider not doing a test on adding the record, perhaps have a pingRegistry route that does this through API.
752+
if err := m.PingRegistry(registry); err != nil {
753+
log.Error(err)
754+
return ErrCannotPingRegistry
755+
}
756+
757+
if _, err := r.Table(tblNameRegistries).Insert(registry).RunWrite(m.session); err != nil {
758+
return err
759+
}
717760
m.logEvent("add-registry", fmt.Sprintf("name=%s endpoint=%s", registry.Name, registry.Addr), []string{"registry"})
718761

719762
return nil
720763
}
721764

722765
func (m DefaultManager) RemoveRegistry(registry *shipyard.Registry) error {
723766
res, err := r.Table(tblNameRegistries).Get(registry.ID).Delete().Run(m.session)
767+
defer res.Close()
724768
if err != nil {
725769
return err
726770
}
@@ -736,6 +780,7 @@ func (m DefaultManager) RemoveRegistry(registry *shipyard.Registry) error {
736780

737781
func (m DefaultManager) Registries() ([]*shipyard.Registry, error) {
738782
res, err := r.Table(tblNameRegistries).OrderBy(r.Asc("name")).Run(m.session)
783+
defer res.Close()
739784
if err != nil {
740785
return nil, err
741786
}
@@ -745,21 +790,18 @@ func (m DefaultManager) Registries() ([]*shipyard.Registry, error) {
745790
return nil, err
746791
}
747792

748-
registries := []*shipyard.Registry{}
749-
for _, r := range regs {
750-
reg, err := shipyard.NewRegistry(r.ID, r.Name, r.Addr)
751-
if err != nil {
752-
return nil, err
793+
for _, registry := range regs {
794+
if err := registry.InitRegistryClient(); err != nil {
795+
log.Errorf("%s", err.Error())
753796
}
754-
755-
registries = append(registries, reg)
756797
}
757798

758-
return registries, nil
799+
return regs, nil
759800
}
760801

761-
func (m DefaultManager) Registry(name string) (*shipyard.Registry, error) {
762-
res, err := r.Table(tblNameRegistries).Filter(map[string]string{"name": name}).Run(m.session)
802+
func (m DefaultManager) Registry(id string) (*shipyard.Registry, error) {
803+
res, err := r.Table(tblNameRegistries).Filter(map[string]string{"id": id}).Run(m.session)
804+
defer res.Close()
763805
if err != nil {
764806
return nil, err
765807

@@ -772,12 +814,35 @@ func (m DefaultManager) Registry(name string) (*shipyard.Registry, error) {
772814
return nil, err
773815
}
774816

775-
registry, err := shipyard.NewRegistry(reg.ID, reg.Name, reg.Addr)
817+
if err := reg.InitRegistryClient(); err != nil {
818+
log.Errorf("%s", err.Error())
819+
return reg, err
820+
}
821+
822+
return reg, nil
823+
}
824+
825+
func (m DefaultManager) RegistryByAddress(addr string) (*shipyard.Registry, error) {
826+
res, err := r.Table(tblNameRegistries).Filter(map[string]string{"addr": addr}).Run(m.session)
827+
defer res.Close()
776828
if err != nil {
777829
return nil, err
778830
}
831+
if res.IsNil() {
832+
log.Debugf("Could not find registry with address %s", addr)
833+
return nil, ErrRegistryDoesNotExist
834+
}
835+
var reg *shipyard.Registry
836+
if err := res.One(&reg); err != nil {
837+
return nil, err
838+
}
839+
840+
if err := reg.InitRegistryClient(); err != nil {
841+
log.Error(err)
842+
return reg, err
843+
}
779844

780-
return registry, nil
845+
return reg, nil
781846
}
782847

783848
func (m DefaultManager) CreateConsoleSession(c *shipyard.ConsoleSession) error {

Diff for: controller/mock_test/manager_mock.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ func (m MockManager) Registry(name string) (*shipyard.Registry, error) {
148148
func (m MockManager) RemoveRegistry(registry *shipyard.Registry) error {
149149
return nil
150150
}
151-
151+
func (m MockManager) RegistryByAddress(addr string) (*shipyard.Registry, error){
152+
return nil, nil
153+
}
152154
func (m MockManager) Nodes() ([]*shipyard.Node, error) {
153155
return []*shipyard.Node{
154156
TestNode,

0 commit comments

Comments
 (0)