Skip to content

Commit e304263

Browse files
ziwangjYangtao-Hua
authored andcommitted
Revert "Add credential to OpenDataChannel request"
This reverts commit 5c7e9585ae628c7f8ba6b0c281d0d05ca6351a1c. cr: https://code.amazon.com/reviews/CR-160901328
1 parent 80869bb commit e304263

File tree

15 files changed

+34
-321
lines changed

15 files changed

+34
-321
lines changed

src/communicator/mocks/IWebSocketChannel.go

Lines changed: 5 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/communicator/websocketchannel.go

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ package communicator
1616

1717
import (
1818
"errors"
19-
"net/http"
20-
"net/url"
2119
"sync"
2220
"time"
2321

24-
"github.com/aws/aws-sdk-go/aws/signer/v4"
2522
"github.com/aws/session-manager-plugin/src/config"
2623
"github.com/aws/session-manager-plugin/src/log"
2724
"github.com/aws/session-manager-plugin/src/websocketutil"
@@ -30,7 +27,7 @@ import (
3027

3128
// IWebSocketChannel is the interface for DataChannel.
3229
type IWebSocketChannel interface {
33-
Initialize(log log.T, channelUrl string, channelToken string, region string, signer *v4.Signer)
30+
Initialize(log log.T, channelUrl string, channelToken string)
3431
Open(log log.T) error
3532
Close(log log.T) error
3633
SendMessage(log log.T, input []byte, inputType int) error
@@ -52,8 +49,6 @@ type WebSocketChannel struct {
5249
writeLock *sync.Mutex
5350
Connection *websocket.Conn
5451
ChannelToken string
55-
Region string
56-
Signer *v4.Signer
5752
}
5853

5954
// GetChannelToken gets the channel token
@@ -82,11 +77,9 @@ func (webSocketChannel *WebSocketChannel) SetOnMessage(onMessageHandler func([]b
8277
}
8378

8479
// Initialize initializes websocket channel fields
85-
func (webSocketChannel *WebSocketChannel) Initialize(log log.T, channelUrl string, channelToken string, region string, signer *v4.Signer) {
80+
func (webSocketChannel *WebSocketChannel) Initialize(log log.T, channelUrl string, channelToken string) {
8681
webSocketChannel.ChannelToken = channelToken
8782
webSocketChannel.Url = channelUrl
88-
webSocketChannel.Region = region
89-
webSocketChannel.Signer = signer
9083
}
9184

9285
// StartPings starts the pinging process to keep the websocket channel alive.
@@ -128,47 +121,6 @@ func (webSocketChannel *WebSocketChannel) SendMessage(log log.T, input []byte, i
128121
return err
129122
}
130123

131-
// getV4SignatureHeader gets the signed header.
132-
func (webSocketChannel *WebSocketChannel) getV4SignatureHeader(log log.T, Url string) (http.Header, error) {
133-
request, err := http.NewRequest("GET", Url, nil)
134-
135-
if webSocketChannel.Signer != nil {
136-
_, err = webSocketChannel.Signer.Sign(request, nil, config.ServiceName, webSocketChannel.Region, time.Now())
137-
if err != nil {
138-
log.Errorf("Failed to sign websocket, %v", err)
139-
}
140-
}
141-
return request.Header, err
142-
}
143-
144-
// isPresignedURL check is the url presigned.
145-
func isPresignedURL(rawURL string) (bool, error) {
146-
parsedURL, err := url.Parse(rawURL)
147-
if err != nil {
148-
return false, err
149-
}
150-
151-
queryParams := parsedURL.Query()
152-
153-
presignedURLParams := []string{
154-
"X-Amz-Algorithm",
155-
"X-Amz-Credential",
156-
"X-Amz-Date",
157-
"X-Amz-Expires",
158-
"X-Amz-SignedHeaders",
159-
"X-Amz-Signature",
160-
"X-Amz-Security-Token",
161-
}
162-
163-
for _, param := range presignedURLParams {
164-
if _, exists := queryParams[param]; exists {
165-
return true, nil
166-
}
167-
}
168-
169-
return false, nil
170-
}
171-
172124
// Close closes the corresponding connection.
173125
func (webSocketChannel *WebSocketChannel) Close(log log.T) error {
174126

@@ -187,22 +139,9 @@ func (webSocketChannel *WebSocketChannel) Close(log log.T) error {
187139
func (webSocketChannel *WebSocketChannel) Open(log log.T) error {
188140
// initialize the write mutex
189141
webSocketChannel.writeLock = &sync.Mutex{}
190-
presigned, err := isPresignedURL(webSocketChannel.Url)
191-
if err != nil {
192-
return err
193-
}
194-
195-
var header http.Header
196-
if !presigned {
197-
header, err = webSocketChannel.getV4SignatureHeader(log, webSocketChannel.Url)
198-
if err != nil {
199-
log.Errorf("Failed to get the v4 signature, %v", err)
200-
}
201-
}
202142

203-
ws, err := websocketutil.NewWebsocketUtil(log, nil).OpenConnection(webSocketChannel.Url, header)
143+
ws, err := websocketutil.NewWebsocketUtil(log, nil).OpenConnection(webSocketChannel.Url)
204144
if err != nil {
205-
log.Errorf("Failed to open WebSocket connection: %v", err)
206145
return err
207146
}
208147
webSocketChannel.Connection = ws

src/communicator/websocketchannel_test.go

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ import (
2020
"net/http"
2121
"net/http/httptest"
2222
"net/url"
23-
"strings"
2423
"sync"
2524
"testing"
2625

27-
"github.com/aws/aws-sdk-go/aws/credentials"
28-
"github.com/aws/aws-sdk-go/aws/signer/v4"
2926
"github.com/aws/session-manager-plugin/src/log"
3027
"github.com/gorilla/websocket"
3128
"github.com/stretchr/testify/assert"
@@ -38,8 +35,6 @@ var (
3835
defaultStreamUrl = "streamUrl"
3936
defaultError = errors.New("Default Error")
4037
defaultMessage = []byte("Default Message")
41-
defaultRegion = "us-east-1"
42-
mockSigner = &v4.Signer{Credentials: credentials.NewStaticCredentials("AKID", "SECRET", "SESSION")}
4338
)
4439

4540
type ErrorCallbackWrapper struct {
@@ -146,11 +141,10 @@ func TestWebsocketChannel_SetOnMessage(t *testing.T) {
146141
func TestWebsocketchannel_Initialize(t *testing.T) {
147142
t.Log("Starting test: webSocketChannel.Initialize")
148143
channel := &WebSocketChannel{}
149-
channel.Initialize(mockLogger, defaultStreamUrl, defaultChannelToken, defaultRegion, mockSigner)
144+
channel.Initialize(mockLogger, defaultStreamUrl, defaultChannelToken)
150145

151146
assert.Equal(t, defaultStreamUrl, channel.Url)
152147
assert.Equal(t, defaultChannelToken, channel.ChannelToken)
153-
assert.Equal(t, mockSigner, channel.Signer)
154148
}
155149

156150
func TestOpenCloseWebSocketChannel(t *testing.T) {
@@ -175,48 +169,6 @@ func TestOpenCloseWebSocketChannel(t *testing.T) {
175169
t.Log("Ending test: TestOpenCloseWebSocketChannel")
176170
}
177171

178-
func TestOpenWebSocketChannelWithPresignedURL(t *testing.T) {
179-
t.Log("Starting test: TestOpenWebSocketChannelWithPresignedURL")
180-
srv := httptest.NewServer(http.HandlerFunc(handlerToBeTested))
181-
u, _ := url.Parse(srv.URL)
182-
u.Scheme = "ws"
183-
var log = log.NewMockLog()
184-
185-
query := u.Query()
186-
query.Set("X-Amz-Signature", "SAMPLE_SIGNATURE")
187-
u.RawQuery = query.Encode()
188-
189-
websocketchannel := WebSocketChannel{
190-
Url: u.String(),
191-
Signer: nil,
192-
}
193-
194-
err := websocketchannel.Open(log)
195-
assert.Nil(t, err, "Error opening the websocket connection.")
196-
assert.NotNil(t, websocketchannel.Connection, "Open connection failed.")
197-
assert.True(t, websocketchannel.IsOpen, "IsOpen is not set to true.")
198-
assert.True(t, strings.Contains(websocketchannel.Url, "SAMPLE_SIGNATURE"),
199-
"URL not included signature as expected")
200-
201-
err = websocketchannel.Close(log)
202-
assert.Nil(t, err, "Error closing the websocket connection.")
203-
assert.False(t, websocketchannel.IsOpen, "IsOpen is not set to false.")
204-
t.Log("Ending test: TestOpenCloseWebSocketChannel")
205-
}
206-
207-
func TestOpenWebSocketChannelWithInvalidURL(t *testing.T) {
208-
t.Log("Starting test: TestOpenWebSocketChannelWithInvalidURL")
209-
var log = log.NewMockLog()
210-
websocketchannel := WebSocketChannel{
211-
Url: "invalid_url",
212-
Signer: nil,
213-
}
214-
215-
err := websocketchannel.Open(log)
216-
assert.NotNil(t, err, "malformed ws or wss URL.")
217-
assert.Nil(t, websocketchannel.Connection, "Open connection failed.")
218-
}
219-
220172
func TestReadWriteTextToWebSocketChannel(t *testing.T) {
221173
t.Log("Starting test: TestReadWriteWebSocketChannel ")
222174
srv := httptest.NewServer(http.HandlerFunc(handlerToBeTested))

src/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package config
1717
import "time"
1818

1919
const (
20-
ServiceName = "ssmmessages"
2120
RolePublishSubscribe = "publish_subscribe"
2221
MessageSchemaVersion = "1.0"
2322
DefaultTransmissionTimeout = 200 * time.Millisecond

0 commit comments

Comments
 (0)