Skip to content

Commit

Permalink
fix: json
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Lin <[email protected]>
  • Loading branch information
Ezzahhh committed Dec 30, 2024
1 parent b73f7d2 commit 87fed2f
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions internal/provider/ephemeral_ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"encoding/json"
"fmt"
"strconv"

Expand Down Expand Up @@ -109,10 +110,19 @@ func (d *SSMEphemeral) Open(ctx context.Context, req ephemeral.OpenRequest, resp
return
}

sessionID := forkResult.Session.SessionId

// Encode the session ID string as JSON
sessionIDBytes, err := json.Marshal(sessionID)
if err != nil {
resp.Diagnostics.AddError("Failed to serialise JSON for session ID", fmt.Sprintf("Error: %s", err))
return
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.Result.Set(ctx, &data)...)
resp.Private.SetKey(ctx, "tunnel_pid", []byte(strconv.Itoa(forkResult.Command.Process.Pid)))
resp.Private.SetKey(ctx, "session_id", []byte(forkResult.Session.SessionId))
resp.Private.SetKey(ctx, "session_id", []byte(sessionIDBytes))

Check failure on line 125 in internal/provider/ephemeral_ssm.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary conversion (unconvert)

Check failure on line 125 in internal/provider/ephemeral_ssm.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary conversion (unconvert)

Check failure on line 125 in internal/provider/ephemeral_ssm.go

View workflow job for this annotation

GitHub Actions / Build

unnecessary conversion (unconvert)
resp.Private.SetKey(ctx, "ssm_region", []byte(data.SSMRegion.ValueString()))
}

Expand All @@ -135,8 +145,17 @@ func (d *SSMEphemeral) Close(ctx context.Context, req ephemeral.CloseRequest, re
return
}

sessionID, _ := req.Private.GetKey(ctx, "session_id")
sessionIDBytes, _ := req.Private.GetKey(ctx, "session_id")
var sessionID string
if err := json.Unmarshal(sessionIDBytes, &sessionID); err != nil {
resp.Diagnostics.AddError("Failed to decode session ID JSON", fmt.Sprintf("Error: %s", err))
return
}
ssmRegion, _ := req.Private.GetKey(ctx, "ssm_region")
if len(sessionID) < 1 {
resp.Diagnostics.AddWarning("Cannot close SSM tunnel", "SessionID length received is 0")
return
}
awsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(string(ssmRegion)))
if err != nil {
resp.Diagnostics.AddError("Failed to load AWS config", fmt.Sprintf("Error: %s", err))
Expand Down

0 comments on commit 87fed2f

Please sign in to comment.