-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage interface with beginning of implementation for git
- Loading branch information
Showing
5 changed files
with
294 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters