Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitize id for project access token resource #409

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions rollbar/resource_project_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package rollbar

import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"strconv"
"strings"
Expand All @@ -34,6 +36,12 @@ import (
"github.com/rs/zerolog/log"
)

func getMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}

func resourceProjectAccessToken() *schema.Resource {
return &schema.Resource{
CreateContext: resourceProjectAccessTokenCreate,
Expand Down Expand Up @@ -156,15 +164,15 @@ func resourceProjectAccessTokenCreate(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

d.SetId(pat.AccessToken)

d.SetId(getMD5Hash(pat.AccessToken))
mustSet(d, "access_token", pat.AccessToken)
return resourceProjectAccessTokenRead(ctx, d, m)
}

func resourceProjectAccessTokenRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

accessToken := d.Id()
accessToken := d.Get("access_token").(string)
projectID := d.Get("project_id").(int)
l := log.With().
Str("accessToken", accessToken).
Expand Down Expand Up @@ -195,7 +203,7 @@ func resourceProjectAccessTokenRead(ctx context.Context, d *schema.ResourceData,
}

func resourceProjectAccessTokenUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
accessToken := d.Id()
accessToken := d.Get("access_token").(string)
projectID := d.Get("project_id").(int)
size := d.Get("rate_limit_window_size").(int)
count := d.Get("rate_limit_window_count").(int)
Expand All @@ -220,7 +228,7 @@ func resourceProjectAccessTokenUpdate(ctx context.Context, d *schema.ResourceDat
}

func resourceProjectAccessTokenDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
accessToken := d.Id()
accessToken := d.Get("access_token").(string)
projectID := d.Get("project_id").(int)

l := log.With().
Expand Down Expand Up @@ -258,6 +266,7 @@ func resourceProjectAccessTokenImporter(_ context.Context, d *schema.ResourceDat
Str("access_token", accessToken).
Send()
mustSet(d, "project_id", projectID)
d.SetId(accessToken)
mustSet(d, "access_token", accessToken)
d.SetId(getMD5Hash(accessToken))
return []*schema.ResourceData{d}, nil
}
12 changes: 6 additions & 6 deletions rollbar/test1/resource_project_access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ func (s *AccSuite) TestAccTokenDeleteOnAPIBeforeApply() {
// properties.
func (s *AccSuite) checkProjectAccessToken(resourceName string) resource.TestCheckFunc {
return func(ts *terraform.State) error {
accessToken, err := s.getResourceIDString(ts, resourceName)
if err != nil {
return err
}
rs, ok := ts.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}
accessToken := rs.Primary.Attributes["access_token"]
if accessToken == "" {
return fmt.Errorf("access token is empty")
}
projectIDString := rs.Primary.Attributes["project_id"]
projectID, err := strconv.Atoi(projectIDString)
if err != nil {
Expand Down Expand Up @@ -506,7 +506,7 @@ func (s *AccSuite) checkProjectAccessToken(resourceName string) resource.TestChe
// project access token is present in the list of all project access tokens.
func (s *AccSuite) checkProjectAccessTokenInTokenList(rn string) resource.TestCheckFunc {
return func(ts *terraform.State) error {
accessToken, err := s.getResourceIDString(ts, rn)
accessToken, err := s.getResourceAttrString(ts, rn, "access_token")
s.Nil(err)
projectID, err := s.getResourceAttrInt(ts, rn, "project_id")
s.Nil(err)
Expand All @@ -531,7 +531,7 @@ func importIdProjectAccessToken(resourceName string) resource.ImportStateIdFunc
return "", fmt.Errorf("not found: %s", resourceName)
}
projectID := rs.Primary.Attributes["project_id"]
accessToken := rs.Primary.ID
accessToken := rs.Primary.Attributes["access_token"]

return fmt.Sprintf("%s/%s", projectID, accessToken), nil
}
Expand Down
Loading