Skip to content

Commit

Permalink
chore: various pr review adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobmoellerdev committed Oct 2, 2024
1 parent 024b5e6 commit 0d63147
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 119 deletions.
2 changes: 1 addition & 1 deletion api/credentials/builtin/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package builtin

import (
_ "ocm.software/ocm/api/credentials/builtin/git/identity"
_ "ocm.software/ocm/api/credentials/builtin/github"
_ "ocm.software/ocm/api/tech/git/identity"
)
25 changes: 15 additions & 10 deletions api/oci/extensions/repositories/git/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package git

import (
"fmt"

giturls "github.com/whilp/git-urls"
"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/oci/internal"
Expand Down Expand Up @@ -53,7 +53,7 @@ type RepositorySpec struct {
// If empty, the default is set to HEAD.
Ref string `json:"ref,omitempty"`

// Author is the author of commits generated by the repository. If not set, is defaulted from environment and git
// Author is the author of commits generated by the repository. If not set, it is defaulted from environment and git
// configuration of the host system.
Author *Author `json:"author,omitempty"`

Expand Down Expand Up @@ -102,7 +102,7 @@ func NewRepositorySpec(mode accessobj.AccessMode, url string, opts ...accessio.O
}

func (s *RepositorySpec) IsIntermediate() bool {
return true
return false
}

func (s *RepositorySpec) GetType() string {
Expand All @@ -118,6 +118,12 @@ func (s *RepositorySpec) UniformRepositorySpec() *cpi.UniformRepositorySpec {
Type: Type,
Info: s.URL,
}
url, err := giturls.Parse(s.URL)
if err == nil {
u.Host = url.Host
u.Scheme = url.Scheme
}

return u
}

Expand All @@ -141,15 +147,14 @@ func (s *RepositorySpec) ToClientOptions() git.ClientOptions {
return opts
}

func (s *RepositorySpec) Validate(ctx internal.Context, c credentials.Credentials, context ...credentials.UsageContext) error {
repo, err := New(ctx, s, c)
if err != nil {
return fmt.Errorf("failed to initialize repository for validation: %w", err)
func (s *RepositorySpec) Validate(_ internal.Context, c credentials.Credentials, _ ...credentials.UsageContext) error {
if _, err := giturls.Parse(s.URL); err != nil {
return fmt.Errorf("failed to parse git url: %w", err)
}
defer repo.Close()

if _, err := repo.NamespaceLister().NumNamespaces(""); err != nil {
return fmt.Errorf("failed to list namespaces to validate repository: %w", err)
if _, err := git.AuthFromCredentials(c); err != nil {
return fmt.Errorf("failed to create git authentication from given credentials: %w", err)
}

return nil
}
2 changes: 1 addition & 1 deletion api/oci/internal/uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type UniformRepositorySpec struct {
// Host is the hostname of an oci ref.
Host string `json:"host,omitempty"`
// Info is the file path used to host ctf component versions
Info string `json:"filePath,omitempty"`
Info string `json:"info,omitempty"`

// CreateIfMissing indicates whether a file based or dynamic repo should be created if it does not exist
CreateIfMissing bool `json:"createIfMissing,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/elements/artifactaccess/gitaccess/options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package githubaccess
package gitaccess

import (
"github.com/mandelsoft/goutils/optionutils"
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/elements/artifactaccess/gitaccess/resource.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package githubaccess
package gitaccess

import (
"github.com/mandelsoft/goutils/optionutils"
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/extensions/accessmethods/git/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The artifact content is provided as gnu-zipped tar archive
### Description

This method implements the access of the content of a git commit stored in a
GitHub repository.
git repository.

Supported specification version is `v1`

Expand Down
2 changes: 1 addition & 1 deletion api/ocm/extensions/accessmethods/git/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package git
import (
"fmt"
"io"
"ocm.software/ocm/api/tech/git/identity"

"github.com/go-git/go-git/v5/plumbing"
"github.com/mandelsoft/goutils/errors"
giturls "github.com/whilp/git-urls"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/credentials/builtin/git/identity"
"ocm.software/ocm/api/ocm/cpi/accspeccpi"
"ocm.software/ocm/api/ocm/internal"
techgit "ocm.software/ocm/api/tech/git"
Expand Down
2 changes: 1 addition & 1 deletion api/tech/git/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package git

import (
"errors"
"ocm.software/ocm/api/tech/git/identity"

"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
gssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/credentials/builtin/git/identity"
)

var ErrNoValidGitCredentials = errors.New("no valid credentials found for git authentication")
Expand Down
98 changes: 29 additions & 69 deletions api/tech/git/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,22 @@ func VFSBillyFS(fsToWrap vfs.FileSystem) (billy.Filesystem, error) {
}

return &fs{
vfs: fsToWrap,
FileSystem: fsToWrap,
}, nil
}

type fs struct {
vfs vfs.FileSystem
vfs.FileSystem
}

type file struct {
lock *fslock.Lock
vfsFile vfs.File
}

func (f *file) Name() string {
return f.vfsFile.Name()
}

func (f *file) Write(p []byte) (n int, err error) {
return f.vfsFile.Write(p)
}

func (f *file) Read(p []byte) (n int, err error) {
return f.vfsFile.Read(p)
}

func (f *file) ReadAt(p []byte, off int64) (n int, err error) {
return f.vfsFile.ReadAt(p, off)
}
var _ billy.Filesystem = &fs{}

func (f *file) Seek(offset int64, whence int) (int64, error) {
return f.vfsFile.Seek(offset, whence)
type file struct {
lock *fslock.Lock
vfs.File
}

func (f *file) Close() error {
return f.vfsFile.Close()
}
var _ billy.File = &file{}

func (f *file) Lock() error {
return f.lock.Lock()
Expand All @@ -71,14 +51,10 @@ func (f *file) Unlock() error {
return f.lock.Unlock()
}

func (f *file) Truncate(size int64) error {
return f.vfsFile.Truncate(size)
}

var _ billy.File = &file{}

func (f *fs) Create(filename string) (billy.File, error) {
vfsFile, err := f.vfs.Create(filename)
vfsFile, err := f.FileSystem.Create(filename)
if err != nil {
return nil, err
}
Expand All @@ -93,17 +69,17 @@ func (f *fs) Create(filename string) (billy.File, error) {
// juju vfs only operates on syscalls directly and without interface abstraction its not easy to get the root.
func (f *fs) vfsToBillyFileInfo(vf vfs.File) (billy.File, error) {
var lock *fslock.Lock
if f.vfs == osfs.OsFs {
if f.FileSystem == osfs.OsFs {
lock = fslock.New(fmt.Sprintf("%s.lock", vf.Name()))
} else {
hash := fnv.New32()
_, _ = hash.Write([]byte(f.vfs.Name()))
_, _ = hash.Write([]byte(f.FileSystem.Name()))
temp, err := os.MkdirTemp("", fmt.Sprintf("git-vfs-locks-%x", hash.Sum32()))
if err != nil {
return nil, fmt.Errorf("failed to create temp dir to allow mapping vfs to git (billy) filesystem; "+
"this temporary directory is mandatory because a virtual filesystem cannot be used to accurately depict os syslocks: %w", err)
}
_, components := vfs.Components(f.vfs, vf.Name())
_, components := vfs.Components(f.FileSystem, vf.Name())
lockPath := filepath.Join(
temp,
filepath.Join(components[:len(components)-1]...),
Expand All @@ -117,13 +93,13 @@ func (f *fs) vfsToBillyFileInfo(vf vfs.File) (billy.File, error) {
}

return &file{
vfsFile: vf,
lock: lock,
File: vf,
lock: lock,
}, nil
}

func (f *fs) Open(filename string) (billy.File, error) {
vfsFile, err := f.vfs.Open(filename)
vfsFile, err := f.FileSystem.Open(filename)
if err != nil {
return nil, err
}
Expand All @@ -132,19 +108,19 @@ func (f *fs) Open(filename string) (billy.File, error) {

func (f *fs) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
if flag&os.O_CREATE != 0 {
if err := f.vfs.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
if err := f.FileSystem.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
return nil, err
}
}
vfsFile, err := f.vfs.OpenFile(filename, flag, perm)
vfsFile, err := f.FileSystem.OpenFile(filename, flag, perm)
if err != nil {
return nil, err
}
return f.vfsToBillyFileInfo(vfsFile)
}

func (f *fs) Stat(filename string) (os.FileInfo, error) {
fi, err := f.vfs.Stat(filename)
fi, err := f.FileSystem.Stat(filename)
if errors.Is(err, syscall.ENOENT) {
return nil, os.ErrNotExist
}
Expand All @@ -154,39 +130,31 @@ func (f *fs) Stat(filename string) (os.FileInfo, error) {
func (f *fs) Rename(oldpath, newpath string) error {
dir := filepath.Dir(newpath)
if dir != "." {
if err := f.vfs.MkdirAll(dir, 0o755); err != nil {
if err := f.FileSystem.MkdirAll(dir, 0o755); err != nil {
return err
}
}
return f.vfs.Rename(oldpath, newpath)
}

func (f *fs) Remove(filename string) error {
return f.vfs.Remove(filename)
return f.FileSystem.Rename(oldpath, newpath)
}

func (f *fs) Join(elem ...string) string {
return filepath.Join(elem...)
}

func (f *fs) TempFile(dir, prefix string) (billy.File, error) {
vfsFile, err := vfs.TempFile(f.vfs, dir, prefix)
vfsFile, err := vfs.TempFile(f.FileSystem, dir, prefix)
if err != nil {
return nil, err
}
return f.vfsToBillyFileInfo(vfsFile)
}

func (f *fs) ReadDir(path string) ([]os.FileInfo, error) {
return vfs.ReadDir(f.vfs, path)
}

func (f *fs) MkdirAll(filename string, perm os.FileMode) error {
return f.vfs.MkdirAll(filename, perm)
return vfs.ReadDir(f.FileSystem, path)
}

func (f *fs) Lstat(filename string) (os.FileInfo, error) {
fi, err := f.vfs.Lstat(filename)
fi, err := f.FileSystem.Lstat(filename)
if err != nil {
if errors.Is(err, syscall.ENOENT) {
return nil, os.ErrNotExist
Expand All @@ -195,21 +163,13 @@ func (f *fs) Lstat(filename string) (os.FileInfo, error) {
return fi, err
}

func (f *fs) Symlink(target, link string) error {
return f.vfs.Symlink(target, link)
}

func (f *fs) Readlink(link string) (string, error) {
return f.vfs.Readlink(link)
}

func (f *fs) Chroot(path string) (billy.Filesystem, error) {
fi, err := f.vfs.Stat(path)
fi, err := f.FileSystem.Stat(path)
if os.IsNotExist(err) {
if err = f.vfs.MkdirAll(path, 0o755); err != nil {
if err = f.FileSystem.MkdirAll(path, 0o755); err != nil {
return nil, err
}
fi, err = f.vfs.Stat(path)
fi, err = f.FileSystem.Stat(path)
}

if err != nil {
Expand All @@ -218,21 +178,21 @@ func (f *fs) Chroot(path string) (billy.Filesystem, error) {
return nil, fmt.Errorf("path %s is not a directory", path)
}

chfs, err := projectionfs.New(f.vfs, path)
chfs, err := projectionfs.New(f.FileSystem, path)
if err != nil {
return nil, err
}

return &fs{
vfs: chfs,
FileSystem: chfs,
}, nil
}

func (f *fs) Root() string {
if root := projectionfs.Root(f.vfs); root != "" {
if root := projectionfs.Root(f.FileSystem); root != "" {
return root
}
if canonicalRoot, err := vfs.Canonical(f.vfs, "/", true); err == nil {
if canonicalRoot, err := vfs.Canonical(f.FileSystem, "/", true); err == nil {
return canonicalRoot
}
return "/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ the <code>`+hostpath.IDENTITY_TYPE+`</code> type.`,
}

const (
ID_HOSTNAME = hostpath.ID_HOSTNAME
ID_PATH = "path"
ID_PORT = hostpath.ID_PORT
ID_SCHEME = hostpath.ID_SCHEME
ID_HOSTNAME = hostpath.ID_HOSTNAME
ID_PATHPREFIX = hostpath.ID_PATHPREFIX
ID_PORT = hostpath.ID_PORT
ID_SCHEME = hostpath.ID_SCHEME
)

const (
Expand Down Expand Up @@ -96,7 +96,7 @@ func GetConsumerId(repoURL string) (cpi.ConsumerIdentity, error) {
}

if path != "" {
id[ID_PATH] = path
id[ID_PATHPREFIX] = path
}

id[ID_SCHEME] = scheme
Expand All @@ -117,10 +117,10 @@ func BasicAuthCredentials(username, password string) cpi.Credentials {
}
}

func PublicKeyCredentials(username, publicKey string) cpi.Credentials {
func PrivateKeyCredentials(username, privateKey string) cpi.Credentials {
return cpi.DirectCredentials{
ATTR_USERNAME: username,
ATTR_PRIVATE_KEY: publicKey,
ATTR_PRIVATE_KEY: privateKey,
}
}

Expand Down
Loading

0 comments on commit 0d63147

Please sign in to comment.