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

adding CSP changes to grafana integration #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dkuldeep22
Copy link

No description provided.

Copy link

@gangadharaswamy gangadharaswamy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gangadharaswamy
Copy link

@dkuldeep22 Please add the Datasource CLI commands for CSP OAuth and API token in the README.md.

Copy link

@Margarita-Staneva Margarita-Staneva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one comment. I think CSP API token should be replaced with VMware Cloud services API token.

<a class="btn btn-secondary gf-form-btn" style="flex-grow:1" type="submit" ng-click="ctrl.resetCspApiToken()" ng-show="ctrl.cspApiTokenExists">Reset</a>
<input type="text" class="gf-form-input max-width-24" ng-hide="ctrl.cspApiTokenExists" ng-model="ctrl.current.jsonData.cspAPIToken"/>
<info-popover mode="right-absolute" ng-hide="ctrl.cspApiTokenExists">
Paste your CSP API token here. You can find and manage your tokens on your profile page in the Wavefront app

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this throughout:

Paste your VMware Cloud services API token here. You can find and manage your API tokens on your profile page in the Wavefront app.

@amahajan02
Copy link

Please remove console.log statements from all files. These were added for debugging and should not be checked-in

const CSP_API_TOKEN_URL = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize";
const CSP_OAUTH_TOKEN_URL = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/authorize";

const appSecret = ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused variable. Please remove.

@@ -15,6 +21,9 @@ export function WavefrontDatasource(instanceSettings, $q, backendSrv, templateSr
this.backendSrv = new BackendSrvCancelledRetriesDecorator(backendSrv, $q);
this.templateSrv = templateSrv;
this.defaultRequestTimeoutSecs = 15;
const appId = instanceSettings.jsonData.cspOAuthClientId
const appSecret = instanceSettings.jsonData.cspOAuthClientSecret
const credentials = `Basic ${Buffer.from(`${appId}:${appSecret}`).toString('base64')}`;

Copy link

@amahajan02 amahajan02 Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we trying to do here?
Buffer does not exist and credentials is not getting used anywhere.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use btoa for base64 encoding.
Try this: const credentials = Basic ${btoa(${appId}:${appSecret})};``

const endSecs = dateToEpochSeconds(options.range.to);
console.log("********endSecs", endSecs);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove all console.log statements here.

grant_type: "client_credentials",
}),
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add "Authorization" in headers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Authorization": credentials

@@ -15,6 +21,9 @@ export function WavefrontDatasource(instanceSettings, $q, backendSrv, templateSr
this.backendSrv = new BackendSrvCancelledRetriesDecorator(backendSrv, $q);
this.templateSrv = templateSrv;
this.defaultRequestTimeoutSecs = 15;
const appId = instanceSettings.jsonData.cspOAuthClientId
const appSecret = instanceSettings.jsonData.cspOAuthClientSecret
const credentials = `Basic ${Buffer.from(`${appId}:${appSecret}`).toString('base64')}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use btoa for base64 encoding.
Try this: const credentials = Basic ${btoa(${appId}:${appSecret})};``

} catch(e) {
console.error(e);
}
this.requestConfigProto.headers["Authorization"] = "Bearer " + instanceSettings.jsonData.cspAPIToken;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should set "Authorization" header to access token which we get after making call to CSP_OAUTH_TOKEN_URL. Not directly to csp api token. Please confirm this point from Gangadhar or Sadanand.
If they confirm, then change line#45 from :
.then((json) => console.log(json));
to:
then((accessToken) => this.requestConfigProto.headers["Authorization"] = accessToken)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with OAuth

@@ -515,4 +575,51 @@ export function WavefrontDatasource(instanceSettings, $q, backendSrv, templateSr
return this.backendSrv.datasourceRequest(reqConfig);
};


function refreshToken() {
if (instanceSettings.jsonData.cspAPIToken) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is same as what we are doing at line#35 and 51.
Can we create two functions like: fetchAccessTokenUsingCSPAPIToken and fetchAccessTokenUsingCSPOAuth and use these functions during initialisation and interval update

}

// Execute the function initially
refreshToken();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since, we are already calling it during initialisation at line#33, we can skip this initial call of refreshToken

refreshToken();

// Execute the function every 25 minutes (25 * 60 * 1000 milliseconds)
const interval = setInterval(refreshToken, 25 * 60 * 1000);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not stopping the interval at any time. No need to store the result in interval const. Calling the function should be sufficient.
setInterval(refreshToken, 25 * 60 * 1000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants