grim/port-authority

Start of moving this to a go http client
draft default tip
2020-04-27, Gary Kramlich
dfcf60f69754
Parents 405facbf0f21
Children
Start of moving this to a go http client
  • +23 -16
    README.txt
  • +22 -0
    client.go
  • +5 -0
    go.mod
  • +7 -0
    go.sum
  • +7 -0
    registry.go
  • +26 -0
    registry_test.go
  • +43 -0
    transport.go
  • --- a/README.txt Tue Apr 16 02:25:00 2019 -0500
    +++ b/README.txt Mon Apr 27 21:53:44 2020 -0500
    @@ -1,21 +1,28 @@
    -Installing
    -==========
    +# Port Authority
    -To install
    +Port Authority is a simple golang http client that handles all of the
    +authentication involved when interacting with a container registry. This means
    +it's a dumb client and it's up to you, the user to know the API endpoints you
    +want to hit. At some point in the future this may change, but for now it only
    +handles authentication
    +
    +# Usage
    +
    +```go
    +import (
    + "fmt"
    - * clone the source tree
    - * open Google Chrome
    - * navigate to "chrome://extensions"
    - * check "Developer mode" in the top right
    - * click "Load unpacked extension..."
    - * Navigate to the cloned source tree
    - * Click open
    - * The application is now installed
    + pa "bitbucket.org/rw_grim/port-authority"
    +)
    +
    +func main() {
    + client := pa.NewClient()
    -To Use
    + client.AddRegistry("index.docker.io", "username", "password")
    +
    + resp, _ := client.Get("/v2/username/foo/tags")
    - * Click on the apps button in your bookmarks bar
    - * Click "Docker Manifest"
    - * Enter a docker registry hostname (doesn't work on registry.hub.docker.com)
    - * Explore
    + fmt.Printf("%#v\n", resp)
    +}
    +```
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/client.go Mon Apr 27 21:53:44 2020 -0500
    @@ -0,0 +1,22 @@
    +package pa
    +
    +import (
    + "net/http"
    + "sync"
    +)
    +
    +type Client struct {
    + client *http.Client
    +
    + registryLock sync.Mutex
    + registries map[string]registry
    +}
    +
    +func NewClient() *Client {
    + return &Client{
    + client: &http.Client{
    + Transport: NewTransport(),
    + },
    + registries: map[string]registry{},
    + }
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/go.mod Mon Apr 27 21:53:44 2020 -0500
    @@ -0,0 +1,5 @@
    +module keep.imfreedom.org/grim/port-authority
    +
    +go 1.14
    +
    +require github.com/stretchr/testify v1.3.0
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/go.sum Mon Apr 27 21:53:44 2020 -0500
    @@ -0,0 +1,7 @@
    +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
    +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
    +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
    +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
    +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
    +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
    +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/registry.go Mon Apr 27 21:53:44 2020 -0500
    @@ -0,0 +1,7 @@
    +package pa
    +
    +type registry struct {
    + username string
    + password string
    + token string
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/registry_test.go Mon Apr 27 21:53:44 2020 -0500
    @@ -0,0 +1,26 @@
    +package pa
    +
    +import (
    + "testing"
    +
    + "github.com/stretchr/testify/assert"
    +)
    +
    +func TestAddRepository(t *testing.T) {
    + assert := assert.New(t)
    +
    + c := NewClient()
    + assert.NotNil(c)
    +
    + c.AddRegistry("host", "user", "pass")
    +
    + username, password := c.GetRegistry("host")
    + assert.Equal(username, "user")
    + assert.Equal(password, "pass")
    +
    + c.RemoveRegistry("host")
    +
    + username, password = c.GetRegistry("host")
    + assert.Equal(username, "")
    + assert.Equal(password, "")
    +}
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/transport.go Mon Apr 27 21:53:44 2020 -0500
    @@ -0,0 +1,43 @@
    +package pa
    +
    +import (
    + "fmt"
    + "net/http"
    + "sync"
    +)
    +
    +type Transport struct {
    + Transport http.RoundTripper
    +
    + registryLock sync.Mutex
    + registries map[string]registry
    +}
    +
    +func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
    +}
    +
    +func (t *Transport) AddRegistry(hostname, username, password string) {
    + t.registryLock.Lock()
    + defer t.registryLock.Unlock()
    +
    + t.registries[hostname] = registry{username, password}
    +
    +}
    +
    +func (t *Transport) GetRegistry(hostname string) (string, string) {
    + t.registryLock.Lock()
    + defer t.registryLock.Unlock()
    +
    + if reg, found := c.registries[hostname]; found {
    + return reg.username, reg.password
    + }
    +
    + return "", ""
    +}
    +
    +func (t *Transport) RemoveRegistry(hostname string) {
    + t.registryLock.Lock()
    + defer t.registryLock.Unlock()
    +
    + delete(t.registries, hostname)
    +}