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

Commit

Permalink
Feature/shipyard ready registry v2 (#852)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
borzamircea authored and ehazlett committed Oct 5, 2016
1 parent da4501f commit 8edbeac
Show file tree
Hide file tree
Showing 24 changed files with 901 additions and 163 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ image: media build
release: build image
@docker push shipyard/shipyard:$(TAG)

test: clean
test: clean
@godep go test -v ./...

.PHONY: all build clean media image test release
.PHONY: all build clean media image test release
3 changes: 1 addition & 2 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package auth

import (
"errors"
"golang.org/x/crypto/bcrypt"
"strings"
"time"

"golang.org/x/crypto/bcrypt"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion auth/builtin/builtin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package builtin

import (
"golang.org/x/crypto/bcrypt"
"github.com/shipyard/shipyard/auth"
"golang.org/x/crypto/bcrypt"
)

type (
Expand Down
10 changes: 4 additions & 6 deletions auth/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package ldap

import (
"fmt"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/shipyard/shipyard/auth"
goldap "gopkg.in/ldap.v1"
"strings"
)

type (
Expand Down Expand Up @@ -45,17 +45,15 @@ func (a LdapAuthenticator) Authenticate(username, password, hash string) (bool,
defer l.Close()

dn := fmt.Sprintf("cn=%s,%s", username, a.BaseDN)

if err := l.Bind(dn, password); err != nil {
return false, err
}
if strings.Contains(a.BaseDN, "{username}") {
dn = strings.Replace(a.BaseDN, "{username}", username, -1)
}

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

if err := l.Bind(dn, password); err != nil {
return false, err
}

log.Debugf("ldap authentication successful: username=%s", username)

return true, nil
Expand Down
1 change: 1 addition & 0 deletions controller/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
controller
.idea/*
10 changes: 5 additions & 5 deletions controller/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ func (a *Api) Run() error {
apiRouter.HandleFunc("/api/events", a.purgeEvents).Methods("DELETE")
apiRouter.HandleFunc("/api/registries", a.registries).Methods("GET")
apiRouter.HandleFunc("/api/registries", a.addRegistry).Methods("POST")
apiRouter.HandleFunc("/api/registries/{name}", a.registry).Methods("GET")
apiRouter.HandleFunc("/api/registries/{name}", a.removeRegistry).Methods("DELETE")
apiRouter.HandleFunc("/api/registries/{name}/repositories", a.repositories).Methods("GET")
apiRouter.HandleFunc("/api/registries/{name}/repositories/{repo:.*}", a.repository).Methods("GET")
apiRouter.HandleFunc("/api/registries/{name}/repositories/{repo:.*}", a.deleteRepository).Methods("DELETE")
apiRouter.HandleFunc("/api/registries/{registryId}", a.registry).Methods("GET")
apiRouter.HandleFunc("/api/registries/{registryId}", a.removeRegistry).Methods("DELETE")
apiRouter.HandleFunc("/api/registries/{registryId}/repositories", a.repositories).Methods("GET")
apiRouter.HandleFunc("/api/registries/{registryId}/repositories/{repo:.*}", a.repository).Methods("GET")
apiRouter.HandleFunc("/api/registries/{registryId}/repositories/{repo:.*}", a.deleteRepository).Methods("DELETE")
apiRouter.HandleFunc("/api/servicekeys", a.serviceKeys).Methods("GET")
apiRouter.HandleFunc("/api/servicekeys", a.addServiceKey).Methods("POST")
apiRouter.HandleFunc("/api/servicekeys", a.removeServiceKey).Methods("DELETE")
Expand Down
76 changes: 28 additions & 48 deletions controller/api/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func (a *Api) registry(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")

vars := mux.Vars(r)
name := vars["name"]
id := vars["registryId"]

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

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

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

vars := mux.Vars(r)
name := vars["name"]

if name != "" {
registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

repos, err := registry.Repositories()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(repos); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id := vars["registryId"]

if id == "" {
http.Error(w, "Please pass a valid id", http.StatusNotFound)
return
}
registry, err := a.manager.Registry(id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

repos, err := registry.Repositories()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(repos); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

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

vars := mux.Vars(r)
name := vars["name"]
id := vars["registryId"]
repoName := vars["repo"]

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

func (a *Api) deleteRepository(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
id := vars["registryId"]
repoName := vars["repo"]

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

w.WriteHeader(http.StatusNoContent)
}

func (a *Api) inspectRepository(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
repoName := vars["repo"]

registry, err := a.manager.Registry(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

repo, err := registry.Repository(repoName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(repo); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
95 changes: 80 additions & 15 deletions controller/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"crypto/tls"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/sessions"
"github.com/samalba/dockerclient"
Expand Down Expand Up @@ -37,6 +38,7 @@ const (
)

var (
ErrCannotPingRegistry = errors.New("Cannot ping registry")
ErrLoginFailure = errors.New("invalid username or password")
ErrAccountExists = errors.New("account already exists")
ErrAccountDoesNotExist = errors.New("account does not exist")
Expand Down Expand Up @@ -107,6 +109,7 @@ type (
RemoveRegistry(registry *shipyard.Registry) error
Registries() ([]*shipyard.Registry, error)
Registry(name string) (*shipyard.Registry, error)
RegistryByAddress(addr string) (*shipyard.Registry, error)

CreateConsoleSession(c *shipyard.ConsoleSession) error
RemoveConsoleSession(c *shipyard.ConsoleSession) error
Expand Down Expand Up @@ -701,26 +704,67 @@ func (m DefaultManager) Node(name string) (*shipyard.Node, error) {
return nil, nil
}

func (m DefaultManager) AddRegistry(registry *shipyard.Registry) error {
resp, err := http.Get(fmt.Sprintf("%s/v1/search", registry.Addr))
func (m DefaultManager) PingRegistry(registry *shipyard.Registry) error {

// TODO: Please note the trailing forward slash / which is needed for Artifactory, else you get a 404.
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/", registry.Addr), nil)

if err != nil {
return err
}

req.SetBasicAuth(registry.Username, registry.Password)

var tlsConfig *tls.Config

tlsConfig = nil

if registry.TlsSkipVerify {
tlsConfig = &tls.Config{InsecureSkipVerify: true}
}

// Create unsecured client
trans := &http.Transport{
TLSClientConfig: tlsConfig,
}

client := &http.Client{Transport: trans}

resp, err := client.Do(req)

if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}

if _, err := r.Table(tblNameRegistries).Insert(registry).RunWrite(m.session); err != nil {
return nil
}

func (m DefaultManager) AddRegistry(registry *shipyard.Registry) error {

if err := registry.InitRegistryClient(); err != nil {
return err
}

// TODO: consider not doing a test on adding the record, perhaps have a pingRegistry route that does this through API.
if err := m.PingRegistry(registry); err != nil {
log.Error(err)
return ErrCannotPingRegistry
}

if _, err := r.Table(tblNameRegistries).Insert(registry).RunWrite(m.session); err != nil {
return err
}
m.logEvent("add-registry", fmt.Sprintf("name=%s endpoint=%s", registry.Name, registry.Addr), []string{"registry"})

return nil
}

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

func (m DefaultManager) Registries() ([]*shipyard.Registry, error) {
res, err := r.Table(tblNameRegistries).OrderBy(r.Asc("name")).Run(m.session)
defer res.Close()
if err != nil {
return nil, err
}
Expand All @@ -745,21 +790,18 @@ func (m DefaultManager) Registries() ([]*shipyard.Registry, error) {
return nil, err
}

registries := []*shipyard.Registry{}
for _, r := range regs {
reg, err := shipyard.NewRegistry(r.ID, r.Name, r.Addr)
if err != nil {
return nil, err
for _, registry := range regs {
if err := registry.InitRegistryClient(); err != nil {
log.Errorf("%s", err.Error())
}

registries = append(registries, reg)
}

return registries, nil
return regs, nil
}

func (m DefaultManager) Registry(name string) (*shipyard.Registry, error) {
res, err := r.Table(tblNameRegistries).Filter(map[string]string{"name": name}).Run(m.session)
func (m DefaultManager) Registry(id string) (*shipyard.Registry, error) {
res, err := r.Table(tblNameRegistries).Filter(map[string]string{"id": id}).Run(m.session)
defer res.Close()
if err != nil {
return nil, err

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

registry, err := shipyard.NewRegistry(reg.ID, reg.Name, reg.Addr)
if err := reg.InitRegistryClient(); err != nil {
log.Errorf("%s", err.Error())
return reg, err
}

return reg, nil
}

func (m DefaultManager) RegistryByAddress(addr string) (*shipyard.Registry, error) {
res, err := r.Table(tblNameRegistries).Filter(map[string]string{"addr": addr}).Run(m.session)
defer res.Close()
if err != nil {
return nil, err
}
if res.IsNil() {
log.Debugf("Could not find registry with address %s", addr)
return nil, ErrRegistryDoesNotExist
}
var reg *shipyard.Registry
if err := res.One(&reg); err != nil {
return nil, err
}

if err := reg.InitRegistryClient(); err != nil {
log.Error(err)
return reg, err
}

return registry, nil
return reg, nil
}

func (m DefaultManager) CreateConsoleSession(c *shipyard.ConsoleSession) error {
Expand Down
4 changes: 3 additions & 1 deletion controller/mock_test/manager_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func (m MockManager) Registry(name string) (*shipyard.Registry, error) {
func (m MockManager) RemoveRegistry(registry *shipyard.Registry) error {
return nil
}

func (m MockManager) RegistryByAddress(addr string) (*shipyard.Registry, error){
return nil, nil
}
func (m MockManager) Nodes() ([]*shipyard.Node, error) {
return []*shipyard.Node{
TestNode,
Expand Down
Loading

0 comments on commit 8edbeac

Please sign in to comment.