Skip to content

Commit

Permalink
Merge pull request #2308 from posit-dev/mnv-on-file-creds
Browse files Browse the repository at this point in the history
File credentials service (unplugged at the moment)
  • Loading branch information
marcosnav authored Sep 26, 2024
2 parents 1db45a8 + d957725 commit b77d14d
Show file tree
Hide file tree
Showing 8 changed files with 857 additions and 3 deletions.
14 changes: 12 additions & 2 deletions internal/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ type Credential struct {

type CredentialV0 = Credential

func (c *Credential) ConflictCheck(compareWith Credential) error {
if compareWith.URL == c.URL {
return NewURLCollisionError(c.Name, c.URL)
}
if compareWith.Name == c.Name {
return NewNameCollisionError(c.Name, c.URL)
}
return nil
}

type CredentialRecord struct {
GUID string `json:"guid"`
Version uint `json:"version"`
Expand Down Expand Up @@ -95,7 +105,7 @@ func NewCredentialsService(log logging.Logger) *credentialsService {
// FileCredentialRecordFactory creates a Credential based on the presence of the
// a file containing CONNECT_SERVER and CONNECT_API_KEY.

type fileCredential struct {
type deprcfileCredential struct {
URL string `toml:"url"`
Key string `toml:"key"`
}
Expand All @@ -113,7 +123,7 @@ func (cs *credentialsService) FileCredentialRecordFactory() (*CredentialRecord,
if !exists {
return nil, nil
}
var credential fileCredential
var credential deprcfileCredential
err = util.ReadTOMLFile(filePath, &credential)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *CredentialsTestSuite) createFileCredentials(cs *credentialsService, err
defer f.Close()

enc := toml.NewEncoder(f)
cred := fileCredential{
cred := deprcfileCredential{
URL: fileCred.URL,
Key: fileCred.ApiKey,
}
Expand Down
46 changes: 46 additions & 0 deletions internal/credentials/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type CorruptedError struct {
GUID string
}

func NewCorruptedError(guid string) *CorruptedError {
return &CorruptedError{guid}
}

func (e *CorruptedError) Error() string {
return fmt.Sprintf("credential '%s' is corrupted", e.GUID)
}
Expand All @@ -16,6 +20,10 @@ type LoadError struct {
Err error
}

func NewLoadError(err error) *LoadError {
return &LoadError{err}
}

func (e *LoadError) Error() string {
return fmt.Sprintf("failed to load credentials: %v", e.Err)
}
Expand All @@ -24,6 +32,10 @@ type NotFoundError struct {
GUID string
}

func NewNotFoundError(guid string) *NotFoundError {
return &NotFoundError{GUID: guid}
}

func (e *NotFoundError) Error() string {
return fmt.Sprintf("credential not found: %s", e.GUID)
}
Expand All @@ -34,6 +46,10 @@ type URLCollisionError struct {
URL string
}

func NewURLCollisionError(name, url string) *URLCollisionError {
return &URLCollisionError{name, url}
}

func (e *URLCollisionError) Error() string {
return fmt.Sprintf("URL value conflicts with existing credential (%s) URL: %s", e.Name, e.URL)
}
Expand All @@ -44,6 +60,10 @@ type EnvURLCollisionError struct {
URL string
}

func NewEnvURLCollisionError(name, url string) *EnvURLCollisionError {
return &EnvURLCollisionError{name, url}
}

func (e *EnvURLCollisionError) Error() string {
return fmt.Sprintf("CONNECT_SERVER URL value conflicts with existing credential (%s) URL: %s", e.Name, e.URL)
}
Expand All @@ -54,6 +74,10 @@ type NameCollisionError struct {
URL string
}

func NewNameCollisionError(name, url string) *NameCollisionError {
return &NameCollisionError{name, url}
}

func (e *NameCollisionError) Error() string {
return fmt.Sprintf("Name value conflicts with existing credential (%s) URL: %s", e.Name, e.URL)
}
Expand All @@ -64,6 +88,10 @@ type EnvNameCollisionError struct {
URL string
}

func NewEnvNameCollisionError(name, url string) *EnvNameCollisionError {
return &EnvNameCollisionError{name, url}
}

func (e *EnvNameCollisionError) Error() string {
return fmt.Sprintf("CONNECT_SERVER Name value conflicts with existing credential (%s) URL: %s", e.Name, e.URL)
}
Expand All @@ -73,6 +101,10 @@ type EnvURLDeleteError struct {
GUID string
}

func NewEnvURLDeleteError(guid string) *EnvURLDeleteError {
return &EnvURLDeleteError{guid}
}

func (e *EnvURLDeleteError) Error() string {
return fmt.Sprintf("DELETING an environment credential is not allowed. (GUID=%s)", e.GUID)
}
Expand All @@ -81,6 +113,20 @@ type VersionError struct {
Version uint
}

func NewVersionError(v uint) *VersionError {
return &VersionError{v}
}

func (e *VersionError) Error() string {
return fmt.Sprintf("credential version not supported: %d", e.Version)
}

type IncompleteCredentialError struct{}

func NewIncompleteCredentialError() *IncompleteCredentialError {
return &IncompleteCredentialError{}
}

func (e *IncompleteCredentialError) Error() string {
return "New credentials require non-empty Name, URL and Api Key fields"
}
Loading

0 comments on commit b77d14d

Please sign in to comment.