-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Both inputs gcp-pubsub and httpjson used []byte fields as part of their configurations to receive json blobs. This caused issues because the config values never get parsed properly since literal JSON strings arrived as string and objects as maps, causing errors similar to can not convert 'string' into 'uint8' accessing 'auth.oauth2.google.credentials_json' or can not convert 'object' into 'uint8' accessing 'auth.oauth2.google.credentials_json'. This creates a JSONBlob type that can be unpacked from literal json strings or from config objects into a raw json message. (cherry picked from commit 9022e19) Co-authored-by: Marc Guasch <[email protected]>
- Loading branch information
Showing
8 changed files
with
148 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
) | ||
|
||
// JSONBlob is a custom type that can unpack raw JSON strings or objects into | ||
// a json.RawMessage. | ||
type JSONBlob json.RawMessage | ||
|
||
func (b *JSONBlob) Unpack(v interface{}) error { | ||
switch t := v.(type) { | ||
case string: | ||
*b = []byte(t) | ||
default: | ||
m, err := json.Marshal(v) | ||
if err != nil { | ||
return err | ||
} | ||
*b = m | ||
} | ||
|
||
if !json.Valid(*b) { | ||
return errors.New("the field can't be converted to valid JSON") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConfigJSONBlob(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
config map[string]interface{} | ||
expectedOut []byte | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "successfully unpacks string", | ||
config: map[string]interface{}{ | ||
"jsonBlob": `{"key":"value"}`, | ||
}, | ||
expectedOut: []byte(`{"key":"value"}`), | ||
}, | ||
{ | ||
name: "successfully unpacks map[string]interface{}", | ||
config: map[string]interface{}{ | ||
"jsonBlob": map[string]interface{}{"key": "value"}, | ||
}, | ||
expectedOut: []byte(`{"key":"value"}`), | ||
}, | ||
{ | ||
name: "successfully unpacks MapStr", | ||
config: map[string]interface{}{ | ||
"jsonBlob": MapStr{"key": "value"}, | ||
}, | ||
expectedOut: []byte(`{"key":"value"}`), | ||
}, | ||
{ | ||
name: "fails if can't be converted to json", | ||
config: map[string]interface{}{ | ||
"jsonBlob": `invalid`, | ||
}, | ||
expectedErr: "the field can't be converted to valid JSON accessing 'jsonBlob'", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
cfg := MustNewConfigFrom(tc.config) | ||
conf := struct { | ||
JSONBlob JSONBlob `config:"jsonBlob"` | ||
}{} | ||
err := cfg.Unpack(&conf) | ||
if tc.expectedErr == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tc.expectedErr) | ||
} | ||
assert.EqualValues(t, string(tc.expectedOut), string(conf.JSONBlob)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,24 +329,24 @@ func TestConfigOauth2Validation(t *testing.T) { | |
input: map[string]interface{}{ | ||
"oauth2": map[string]interface{}{ | ||
"provider": "google", | ||
"google.credentials_json": []byte(`{ | ||
"google.credentials_json": `{ | ||
"type": "service_account", | ||
"project_id": "foo", | ||
"private_key_id": "x", | ||
"client_email": "[email protected]", | ||
"client_id": "0" | ||
}`), | ||
}`, | ||
}, | ||
"url": "localhost", | ||
}, | ||
}, | ||
{ | ||
name: "google must fail if credentials_json is not a valid JSON", | ||
expectedErr: "invalid configuration: google.credentials_json must be valid JSON accessing 'oauth2'", | ||
expectedErr: "the field can't be converted to valid JSON accessing 'oauth2.google.credentials_json'", | ||
input: map[string]interface{}{ | ||
"oauth2": map[string]interface{}{ | ||
"provider": "google", | ||
"google.credentials_json": []byte(`invalid`), | ||
"google.credentials_json": `invalid`, | ||
}, | ||
"url": "localhost", | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,23 +287,23 @@ func TestConfigOauth2Validation(t *testing.T) { | |
input: map[string]interface{}{ | ||
"auth.oauth2": map[string]interface{}{ | ||
"provider": "google", | ||
"google.credentials_json": []byte(`{ | ||
"google.credentials_json": `{ | ||
"type": "service_account", | ||
"project_id": "foo", | ||
"private_key_id": "x", | ||
"client_email": "[email protected]", | ||
"client_id": "0" | ||
}`), | ||
}`, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "google must fail if credentials_json is not a valid JSON", | ||
expectedErr: "google.credentials_json must be valid JSON accessing 'auth.oauth2'", | ||
expectedErr: "the field can't be converted to valid JSON accessing 'auth.oauth2.google.credentials_json'", | ||
input: map[string]interface{}{ | ||
"auth.oauth2": map[string]interface{}{ | ||
"provider": "google", | ||
"google.credentials_json": []byte(`invalid`), | ||
"google.credentials_json": `invalid`, | ||
}, | ||
}, | ||
}, | ||
|