Skip to content

Commit

Permalink
Merge pull request #1 from Azanul/credential-crud-remote
Browse files Browse the repository at this point in the history
Credential crud remote
  • Loading branch information
Azanul authored Apr 13, 2023
2 parents b10e859 + 237089c commit bc9bcb4
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 24 deletions.
8 changes: 4 additions & 4 deletions server/handlers/credentials_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (h *Handler) SaveUserCredential(w http.ResponseWriter, req *http.Request, _
return
}

err = provider.SaveUserCredential(&credential)
err = provider.SaveUserCredential(req, &credential)
if err != nil {
h.log.Error(fmt.Errorf("error saving user credentials: %v", err))
http.Error(w, "unable to save user credentials", http.StatusInternalServerError)
Expand Down Expand Up @@ -66,7 +66,7 @@ func (h *Handler) GetUserCredentials(w http.ResponseWriter, req *http.Request, _

h.log.Debug(fmt.Sprintf("page: %d, page size: %d, search: %s, order: %s", page+1, pageSize, search, order))

credentialsPage, err := provider.GetUserCredentials(user.ID, page, pageSize, order, search)
credentialsPage, err := provider.GetUserCredentials(req, user.ID, page, pageSize, order, search)
if err != nil {
h.log.Error(fmt.Errorf("error getting user credentials: %v", err))
http.Error(w, "unable to get user credentials", http.StatusInternalServerError)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (h *Handler) UpdateUserCredential(w http.ResponseWriter, req *http.Request,
return
}

_, err = provider.UpdateUserCredential(credential)
_, err = provider.UpdateUserCredential(req, credential)
if err != nil {
h.log.Error(fmt.Errorf("error getting user credential: %v", err))
http.Error(w, "unable to get user credential", http.StatusInternalServerError)
Expand All @@ -115,7 +115,7 @@ func (h *Handler) DeleteUserCredential(w http.ResponseWriter, req *http.Request,
q := req.URL.Query()

credentialID := uuid.FromStringOrNil(q.Get("credential_id"))
_, err := provider.DeleteUserCredential(credentialID)
_, err := provider.DeleteUserCredential(req, credentialID)
if err != nil {
h.log.Error(fmt.Errorf("error deleting user credential: %v", err))
http.Error(w, "unable to delete user credential", http.StatusInternalServerError)
Expand Down
8 changes: 4 additions & 4 deletions server/models/default_local_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,15 +1081,15 @@ func (l *DefaultLocalProvider) Cleanup() error {
return l.MesheryK8sContextPersister.DB.Migrator().DropTable(&MesheryFilter{})
}

func (l *DefaultLocalProvider) SaveUserCredential(credential *Credential) error {
func (l *DefaultLocalProvider) SaveUserCredential(_ *http.Request, credential *Credential) error {
result := l.GetGenericPersister().Table("credentials").Create(&credential)
if result.Error != nil {
return fmt.Errorf("error saving user credentials: %v", result.Error)
}
return nil
}

func (l *DefaultLocalProvider) GetUserCredentials(userID string, page, pageSize int, search, order string) (*CredentialsPage, error) {
func (l *DefaultLocalProvider) GetUserCredentials(_ *http.Request, userID string, page, pageSize int, search, order string) (*CredentialsPage, error) {
result := l.GetGenericPersister().Select("*").Where("user_id=? and deleted_at is NULL", userID)
if result.Error != nil {
return nil, result.Error
Expand Down Expand Up @@ -1128,7 +1128,7 @@ func (l *DefaultLocalProvider) GetUserCredentials(userID string, page, pageSize
return credentialsPage, nil
}

func (l *DefaultLocalProvider) UpdateUserCredential(credential *Credential) (*Credential, error) {
func (l *DefaultLocalProvider) UpdateUserCredential(_ *http.Request, credential *Credential) (*Credential, error) {
updatedCredential := &Credential{}
if err := l.GetGenericPersister().Model(*updatedCredential).Where("user_id = ? AND id = ? AND deleted_at is NULL", credential.UserID, credential.ID).Updates(credential); err != nil {
return nil, fmt.Errorf("error updating user credential: %v", err)
Expand All @@ -1140,7 +1140,7 @@ func (l *DefaultLocalProvider) UpdateUserCredential(credential *Credential) (*Cr
return updatedCredential, nil
}

func (l *DefaultLocalProvider) DeleteUserCredential(credentialID uuid.UUID) (*Credential, error) {
func (l *DefaultLocalProvider) DeleteUserCredential(_ *http.Request, credentialID uuid.UUID) (*Credential, error) {
delCredential := &Credential{}
if err := l.GetGenericPersister().Model(&Credential{}).Where("id = ?", credentialID).Update("deleted_at", time.Now()).Error; err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions server/models/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ type Provider interface {
SaveConnection(req *http.Request, conn *Connection, token string, skipTokenCheck bool) error
DeleteMesheryConnection() error

SaveUserCredential(credential *Credential) error
GetUserCredentials(userID string, page, pageSize int, search, order string) (*CredentialsPage, error)
UpdateUserCredential(credential *Credential) (*Credential, error)
DeleteUserCredential(credentialID uuid.UUID) (*Credential, error)
SaveUserCredential(req *http.Request, credential *Credential) error
GetUserCredentials(req *http.Request, userID string, page, pageSize int, search, order string) (*CredentialsPage, error)
UpdateUserCredential(req *http.Request, credential *Credential) (*Credential, error)
DeleteUserCredential(req *http.Request, credentialID uuid.UUID) (*Credential, error)
}
148 changes: 136 additions & 12 deletions server/models/remote_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3402,41 +3402,165 @@ func (l *RemoteProvider) GetKubeClient() *mesherykube.Client {
}

// SaveCredential - to save a creadential for an integration
// TODO
func (l *RemoteProvider) SaveUserCredential(_ *Credential) error {
func (l *RemoteProvider) SaveUserCredential(req *http.Request, credential *Credential) error {
if !l.Capabilities.IsSupported(PersistCredentials) {
logrus.Error("operation not available")
return ErrInvalidCapability("PersistCredentials", l.ProviderName)
}
return nil

ep, _ := l.Capabilities.GetEndpointForFeature(PersistCredentials)
_creds, err := json.Marshal(credential)
if err != nil {
return err
}
bf := bytes.NewBuffer(_creds)
remoteProviderURL, _ := url.Parse(l.RemoteProviderURL + ep)
cReq, _ := http.NewRequest(http.MethodPost, remoteProviderURL.String(), bf)
tokenString, _ := l.GetToken(req)
if err != nil {
logrus.Error("error getting token: ", err)
return err
}

resp, err := l.DoRequest(cReq, tokenString)
if err != nil {
return ErrFetch(err, "Save Credential", resp.StatusCode)
}
defer resp.Body.Close()

bdr, err := io.ReadAll(resp.Body)
if err != nil {
return ErrDataRead(err, "Save Credential")
}

if resp.StatusCode == http.StatusOK {
return nil
}

return ErrFetch(fmt.Errorf("failed to save the credential"), fmt.Sprint(bdr), resp.StatusCode)
}

// GetCredentials - to get saved credentials
// TODO
func (l *RemoteProvider) GetUserCredentials(_ string, _, _ int, _, _ string) (*CredentialsPage, error) {
func (l *RemoteProvider) GetUserCredentials(req *http.Request, _, page, pageSize, search, order string) (*CredentialsPage, error) {
if !l.Capabilities.IsSupported(PersistCredentials) {
logrus.Error("operation not available")
return nil, ErrInvalidCapability("PersistCredentials", l.ProviderName)
}
return nil, nil
ep, _ := l.Capabilities.GetEndpointForFeature(PersistSMPTestProfile)

remoteProviderURL, _ := url.Parse(l.RemoteProviderURL + ep)
q := remoteProviderURL.Query()
q.Add("page", page)
q.Add("pageSize", pageSize)
q.Add("search", search)
q.Add("order", order)

remoteProviderURL.RawQuery = q.Encode()
logrus.Debugf("Making request to : %s", remoteProviderURL.String())
cReq, _ := http.NewRequest(http.MethodGet, remoteProviderURL.String(), nil)
tokenString, err := l.GetToken(req)
if err != nil {
return nil, err
}
resp, err := l.DoRequest(cReq, tokenString)
if err != nil {
return nil, ErrFetch(err, "Perf Test Config Page", resp.StatusCode)
}
defer resp.Body.Close()

bdr, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, ErrFetch(fmt.Errorf("could not retrieve list of test profiles: %d", resp.StatusCode), fmt.Sprint(bdr), resp.StatusCode)
}

var cp CredentialsPage
if err = json.Unmarshal(bdr, &cp); err != nil {
return nil, err
}
return &cp, nil
}

// UpdateUserCredential - to update an existing credential
// TODO
func (l *RemoteProvider) UpdateUserCredential(_ *Credential) (*Credential, error) {
func (l *RemoteProvider) UpdateUserCredential(req *http.Request, credential *Credential) (*Credential, error) {
if !l.Capabilities.IsSupported(PersistCredentials) {
logrus.Error("operation not available")
return nil, ErrInvalidCapability("PersistCredentials", l.ProviderName)
}
return nil, nil
ep, _ := l.Capabilities.GetEndpointForFeature(PersistCredentials)
_creds, err := json.Marshal(credential)
if err != nil {
return nil, err
}
bf := bytes.NewBuffer(_creds)
remoteProviderURL, _ := url.Parse(l.RemoteProviderURL + ep)
cReq, _ := http.NewRequest(http.MethodPost, remoteProviderURL.String(), bf)
tokenString, _ := l.GetToken(req)
if err != nil {
logrus.Error("error getting token: ", err)
return nil, err
}

resp, err := l.DoRequest(cReq, tokenString)
if err != nil {
return nil, ErrFetch(err, "Update Credential", resp.StatusCode)
}
defer resp.Body.Close()

bdr, err := io.ReadAll(resp.Body)
if err != nil {
return nil, ErrDataRead(err, "Update Credential")
}

if resp.StatusCode == http.StatusOK {
var cred Credential
if err = json.Unmarshal(bdr, &cred); err != nil {
return nil, err
}
return &cred, nil
}

return nil, ErrFetch(fmt.Errorf("failed to update the credential"), fmt.Sprint(bdr), resp.StatusCode)
}

// DeleteUserCredential - to delete a saved credential
// TODO
func (l *RemoteProvider) DeleteUserCredential(_ uuid.UUID) (*Credential, error) {
func (l *RemoteProvider) DeleteUserCredential(req *http.Request, credentialID uuid.UUID) (*Credential, error) {
if !l.Capabilities.IsSupported(PersistCredentials) {
logrus.Error("operation not available")
return nil, ErrInvalidCapability("PersistCredentials", l.ProviderName)
}
return nil, nil

ep, _ := l.Capabilities.GetEndpointForFeature(PersistMesheryFilters)

logrus.Infof("attempting to delete credential from cloud for id: %s", credentialID)

remoteProviderURL, _ := url.Parse(fmt.Sprintf("%s%s/%s", l.RemoteProviderURL, ep, credentialID))
logrus.Debugf("constructed credential url: %s", remoteProviderURL.String())
cReq, _ := http.NewRequest(http.MethodDelete, remoteProviderURL.String(), nil)

tokenString, err := l.GetToken(req)
if err != nil {
return nil, err
}
resp, err := l.DoRequest(cReq, tokenString)
if err != nil {
logrus.Errorf("unable to delete credential: %v", err)
return nil, ErrDelete(err, "Credential: "+credentialID.String(), resp.StatusCode)
}
defer resp.Body.Close()

bdr, err := io.ReadAll(resp.Body)
if err != nil {
return nil, ErrDataRead(err, "Credential: "+credentialID.String())
}

if resp.StatusCode == http.StatusOK {
logrus.Infof("credential successfully deleted from remote provider")
var cred Credential
if err = json.Unmarshal(bdr, &cred); err != nil {
return nil, err
}
return &cred, nil
}
logrus.Errorf("error while deleting credential: %s", bdr)
return nil, ErrDelete(fmt.Errorf("error while deleting credential: %s", bdr), fmt.Sprint(bdr), resp.StatusCode)
}

0 comments on commit bc9bcb4

Please sign in to comment.