Skip to content

Commit

Permalink
changed oauth token field to securejson field . (#50)
Browse files Browse the repository at this point in the history
* Oauth Implemention started

* checking github actions

* checking github actions

* fixing github actions

* upgraded golang version in backend unit test

* updated frontend packages

* updated all backend packages expect grafan sdk go

* go upgrade completed

* added frontend unit test for Oauth support

* updated frontend packages and github actions versions

* cleaned github actions

* backend complete for changing oauth token field to secret field

* completed changing oauth field to secure filed and changed unit test snapshot
  • Loading branch information
vishalkSimplify authored May 6, 2024
1 parent 3faafb6 commit 6c70f44
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
7 changes: 4 additions & 3 deletions pkg/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ type configArgs struct {
}

// ConnectionURL , generates a vertica connection URL for configArgs. Requires password as input.
func (config *configArgs) ConnectionURL(password string) string {
func (config *configArgs) ConnectionURL(password string , OauthToken string) string {
var tlsmode string
if config.TLSMode == "" {
tlsmode = "none"
} else {
tlsmode = config.TLSMode
}
return fmt.Sprintf("vertica://%s:%s@%s/%s?use_prepared_statements=%d&connection_load_balance=%d&tlsmode=%s&backup_server_node=%s&oauth_access_token=%s", config.User, password, config.URL, config.Database, boolTouint8(config.UsePreparedStmts), boolTouint8(config.UseLoadBalancer), tlsmode, config.BackupServerNode, config.OauthToken)
return fmt.Sprintf("vertica://%s:%s@%s/%s?use_prepared_statements=%d&connection_load_balance=%d&tlsmode=%s&backup_server_node=%s&oauth_access_token=%s", config.User, password, config.URL, config.Database, boolTouint8(config.UsePreparedStmts), boolTouint8(config.UseLoadBalancer), tlsmode, config.BackupServerNode, OauthToken)
}

type queryModel struct {
Expand Down Expand Up @@ -192,12 +192,13 @@ func newDataSourceInstance(_ context.Context, settings backend.DataSourceInstanc
// func newDataSourceInstance(setting backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
var config configArgs
secret := settings.DecryptedSecureJSONData["password"]
OauthToken := settings.DecryptedSecureJSONData["OauthToken"]

err := json.Unmarshal(settings.JSONData, &config)
if err != nil {
return nil, err
}
connStr := config.ConnectionURL(secret)
connStr := config.ConnectionURL(secret,OauthToken)
db, err := sql.Open("vertica", connStr)
if err != nil {
return nil, err
Expand Down
24 changes: 15 additions & 9 deletions src/ConfigEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ const setup = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: ''
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -79,11 +80,11 @@ const setupForHostname = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''
},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: '',
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -124,11 +125,12 @@ const setupForDatabasename = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: ''
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -169,11 +171,12 @@ const setupForUsername = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: '',
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -213,11 +216,12 @@ const setupForPassword = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: 'Demo Password',
OauthToken: ''
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -257,11 +261,12 @@ const setupForSSLMode = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: ''
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -339,11 +344,12 @@ const setupForVerticaConnections = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: ''
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -384,11 +390,12 @@ const setUpBackUpServerNode = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''

},
secureJsonFields: {},
secureJsonData: {
password: '',
OauthToken: ''
},
version: 3,
readOnly: false,
Expand Down Expand Up @@ -429,7 +436,6 @@ const setUpUseBackServer = (propOverrides?: object) => {
useBackupserver: false,
backupServerNode: '',
useOauth: false,
OauthToken: ''
},
secureJsonFields: {},
secureJsonData: {
Expand Down
40 changes: 24 additions & 16 deletions src/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { SSL_MODE_OPTIONS } from './constants';

const { SecretFormField, FormField } = LegacyForms;

export interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions> {}
export interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions> { }

interface State {}
interface State { }

export class ConfigEditor extends PureComponent<Props, State> {
onHostChange = (event: ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -81,25 +81,33 @@ export class ConfigEditor extends PureComponent<Props, State> {


onOauthChange = (event: ChangeEvent<HTMLInputElement>) => {

const { onOptionsChange, options } = this.props;
const jsonData = {
...options.jsonData,
useOauth: event.target.checked,
};
if (!event.target.checked) {
jsonData.OauthToken = '';
}
onOptionsChange({ ...options, jsonData });


onOptionsChange({
...options, jsonData,
secureJsonData: {
...options.secureJsonData,
OauthToken: ''
},
});


};

onOauthTokenChange = (event: ChangeEvent<HTMLInputElement>) => {
const { onOptionsChange, options } = this.props;
const jsonData = {
...options.jsonData,
OauthToken: event.target.value,
};
onOptionsChange({ ...options, jsonData });
onOptionsChange({
...options,
secureJsonData: {
OauthToken: event.target.value,
},
});
};


Expand Down Expand Up @@ -220,7 +228,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
labelWidth={7}
inputWidth={6}
onChange={this.onUserChange}
value={ jsonData.user || ''}
value={jsonData.user || ''}
placeholder="user"
onBlur={() => this.onBlurField(FIELD_TYPES.USER)}
disabled={jsonData.useOauth}
Expand Down Expand Up @@ -275,7 +283,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
value={jsonData.backupServerNode}
placeholder="host1:port,host2:port"
disabled={!jsonData.useBackupserver}
// onBlur={() => this.onBlurField(FIELD_TYPES.BACKUPSERVERNODE)}
// onBlur={() => this.onBlurField(FIELD_TYPES.BACKUPSERVERNODE)}
/>
</div>
<div className="gf-form">
Expand All @@ -292,8 +300,8 @@ export class ConfigEditor extends PureComponent<Props, State> {
label="OAuth Access Token"
labelWidth={15}
inputWidth={21}
onChange={this.onOauthTokenChange}
value={jsonData.OauthToken}
onChange={this.onOauthTokenChange}
value={secureJsonData.OauthToken}
placeholder="OAuth Access Token"
disabled={!jsonData.useOauth}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/__snapshots__/ConfigEditor.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ exports[`Render Render should render component with value in Use Backup Server f
</div>
</div>
<div className="gf-form max-width-30">
<FormField label="OAuth Access Token" labelWidth={15} inputWidth={21} onChange={[Function: onOauthTokenChange]} value="" placeholder="OAuth Access Token" disabled={true} />
<FormField label="OAuth Access Token" labelWidth={15} inputWidth={21} onChange={[Function: onOauthTokenChange]} value={[undefined]} placeholder="OAuth Access Token" disabled={true} />
</div>
</div>
<div className="gf-form-group">
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ export interface MyDataSourceOptions extends DataSourceJsonData {
backupServerNode: string;

useOauth: boolean;

OauthToken: string;
}

/**
* Value that is used in the backend, but never sent over HTTP to the frontend
*/
export interface MySecureJsonData {
password?: string;
OauthToken?: string;
}

export const FIELD_TYPES = {
Expand Down

0 comments on commit 6c70f44

Please sign in to comment.