Skip to content

Commit

Permalink
Storage interface with beginning of implementation for git
Browse files Browse the repository at this point in the history
  • Loading branch information
trevex committed Aug 4, 2020
1 parent a8a7382 commit 0b04f82
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 1 deletion.
127 changes: 127 additions & 0 deletions pkg/storage/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2020 Smorgasbord Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package git

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/kubism/smorgasbord/pkg/storage"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport"
gitstorage "github.com/go-git/go-git/v5/storage"
"github.com/go-git/go-git/v5/storage/memory"
)

const stateName = "smorgasbord.json"

type state = map[string][]storage.Entry

type gitStorage struct {
auth transport.AuthMethod
fs billy.Filesystem
storer gitstorage.Storer
repo *git.Repository
}

func NewStorage(repositoryURL string, auth transport.AuthMethod) (storage.Storage, error) {
s := &gitStorage{
auth: auth,
fs: memfs.New(),
storer: memory.NewStorage(),
}
return s, s.clone(repositoryURL)
}

func (s *gitStorage) Add(id, publicKey string) error {
return nil
}

func (s *gitStorage) Delete(id, publicKey string) error {
return nil
}

func (s *gitStorage) List(id string) ([]storage.Entry, error) {
st, err := s.load()
if err != nil {
return nil, err
}
entries, ok := st[id]
if !ok || entries == nil {
return []storage.Entry{}, nil
}
return entries, nil
}

func (s *gitStorage) Save() error {
return nil
}

func (s *gitStorage) Close() error {
return nil
}

func (s *gitStorage) clone(url string) error {
var err error
s.repo, err = git.Clone(s.storer, s.fs, &git.CloneOptions{
URL: url,
Auth: s.auth,
Depth: 5,
})
return err
}

func (s *gitStorage) load() (state, error) {
if err := s.pull(); err != nil {
return nil, err
}
f, err := s.fs.Open(stateName)
if os.IsNotExist(err) {
return state{}, nil
} else if err != nil {
return nil, err
}
defer func() {
_ = f.Close()
}()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
st := state{}
err = json.Unmarshal(data, &st)
if err != nil {
return nil, err
}
return st, nil
}

func (s *gitStorage) pull() error {
w, err := s.repo.Worktree()
if err != nil {
return err
}
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err == git.NoErrAlreadyUpToDate {
return nil
}
return err
}
30 changes: 30 additions & 0 deletions pkg/storage/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2020 Smorgasbord Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package git

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("GitStorage", func() {
It("contains initial element", func() {
entries, err := gitS.List(testID)
Expect(err).ToNot(HaveOccurred())
Expect(len(entries)).To(BeNumerically(">", 0))
})
})
106 changes: 106 additions & 0 deletions pkg/storage/git/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2020 Smorgasbord Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package git

import (
"fmt"
"io"
"io/ioutil"
"os"
"testing"
"time"

_ "github.com/kubism/smorgasbord/internal/flags"
"github.com/kubism/smorgasbord/pkg/storage"
"github.com/kubism/smorgasbord/pkg/testutil"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

const testID = "[email protected]"

var (
gitServer *testutil.GitServer
gitS storage.Storage
tmpDir string
)

func TestGitStorage(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "pkg/storage/git")
}

var _ = BeforeSuite(func(done Done) {
var err error
tmpDir, err = ioutil.TempDir("", "smorgasbord")
Expect(err).ToNot(HaveOccurred())
gitServer, err = testutil.NewGitServer(tmpDir)
Expect(err).ToNot(HaveOccurred())
// Let's setup the repository for first use
fs := memfs.New()
r, err := git.Init(memory.NewStorage(), fs)
Expect(err).ToNot(HaveOccurred())
_, err = r.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{getRemoteURL()},
})
Expect(err).ToNot(HaveOccurred())
f, err := fs.Create(stateName)
Expect(err).ToNot(HaveOccurred())
_, err = io.WriteString(f, `{ "[email protected]": [{ "publicKey": "...", "allowedIP": "0.0.0.0/0" }] }`)
Expect(err).ToNot(HaveOccurred())
Expect(f.Close()).To(Succeed())
w, err := r.Worktree()
Expect(err).ToNot(HaveOccurred())
_, err = w.Add(stateName)
Expect(err).ToNot(HaveOccurred())
_, err = w.Commit("first commit", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
},
})
Expect(err).ToNot(HaveOccurred())
Expect(r.Push(&git.PushOptions{
RemoteName: "origin",
})).To(Succeed())
// Lastly setup gitStorage
gitS, err = NewStorage(getRemoteURL(), nil)
Expect(err).ToNot(HaveOccurred())
close(done)
}, 240)

var _ = AfterSuite(func() {
if gitServer != nil {
_ = gitServer.Close()
}
if tmpDir != "" {
_ = os.RemoveAll(tmpDir)
}
})

func getRemoteURL() string {
return fmt.Sprintf("http://%s/test.git", gitServer.GetAddr())
}
30 changes: 30 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2020 Smorgasbord Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package storage

type Entry struct {
PublicKey string `json:"publicKey"`
AllowedIP string `json:"allowedIP"`
}

type Storage interface {
Add(id, publicKey string) error
Delete(id, publicKey string) error
List(id string) ([]Entry, error)
Save() error
Close() error
}
2 changes: 1 addition & 1 deletion pkg/testutil/gitserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

var _ = Describe("GitServer", func() {
It("is created successfully and repo is functional", func() {
It("list contains entry", func() {
dir, err := ioutil.TempDir("", "smorgasbord")
Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
Expand Down

0 comments on commit 0b04f82

Please sign in to comment.