Skip to content

Commit 52b4723

Browse files
committed
First commit
Signed-off-by: Francesco Vigo <fvigo@paloaltonetworks.com>
1 parent 377e00f commit 52b4723

5 files changed

Lines changed: 193 additions & 0 deletions

File tree

lib/credentials.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import fetch from 'node-fetch';
2+
3+
// This interface represents AppFramework token data
4+
export interface appFrameworkTokens {
5+
accessToken?: string, // access token
6+
refreshToken?: string, // refresh token
7+
}
8+
9+
// constant URLs, can be overridden
10+
11+
const IDP_TOKEN_URL: string = 'https://api.paloaltonetworks.com/api/oauth2/RequestToken'
12+
const IDP_REVOKE_URL: string = 'https://api.paloaltonetworks.com/api/oauth2/RevokeToken'
13+
const IDP_BASE_URL: string = 'https://identity.paloaltonetworks.com/as/authorization.oauth2'
14+
15+
16+
export class Credentials {
17+
private access_token: string
18+
private refresh_token: string
19+
private client_id: string
20+
private client_secret: string
21+
private idp_token_url: string
22+
private code: string
23+
// TODO: region, instance_id, redirect_uri, scope, token_revoke_url, base_url, etc
24+
25+
constructor(client_id: string, client_secret: string, refresh_token?: string, code?: string, idp_token_url?: string) {
26+
if(!refresh_token && !code) throw(`PanCloudError() Invalid Credentials (code or refresh token missing)`)
27+
this.client_id = client_id
28+
this.client_secret = client_secret
29+
this.refresh_token = refresh_token || undefined
30+
this.idp_token_url = idp_token_url || IDP_TOKEN_URL
31+
this.code = code || undefined
32+
//console.log('this idp =', this.idp_token_url)
33+
}
34+
35+
public get_access_token(): string {
36+
return this.access_token;
37+
}
38+
39+
// version 3.0 with async/await
40+
public async fetch_tokens(): Promise<appFrameworkTokens> {
41+
let res = await fetch(this.idp_token_url, {
42+
method: 'POST',
43+
headers: {
44+
'Content-Type': 'application/json',
45+
'Accept': 'application/json'
46+
},
47+
body: JSON.stringify({
48+
"client_id": this.client_id,
49+
"client_secret": this.client_secret,
50+
"refresh_token": this.refresh_token || undefined,
51+
"code": this.code || undefined
52+
})
53+
})
54+
// console.log('debug:', JSON.stringify({
55+
// "client_id": this.client_id,
56+
// "client_secret": this.client_secret,
57+
// "refresh_token": this.refresh_token || undefined,
58+
// "code": this.code || undefined
59+
// }))
60+
if (res.ok !== true && res.size === 0)
61+
throw(`PanCloudError() ${res.status} ${res.statusText}`)
62+
63+
try {
64+
let r_json = await res.json()
65+
if (r_json.error || r_json.error_description)
66+
throw (`PanCloudError(): ` + await res.text())
67+
let ret: appFrameworkTokens = {
68+
accessToken: r_json.access_token
69+
}
70+
this.access_token = r_json.access_token
71+
72+
if(r_json.refresh_token) { // a new refresh token is returned
73+
this.refresh_token = r_json.refresh_token
74+
ret.refreshToken = r_json.refresh_token
75+
}
76+
//ret.refreshToken = 'test_refresh_token' // TODO: remove this
77+
console.log('Credentials(): Authorization token successfully retrieved')
78+
return ret
79+
} catch (exception) {
80+
throw (`PanCloudError() Invalid JSON: ${exception}`)
81+
}
82+
}
83+
};

lib/exceptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class PanCloudError {
2+
3+
private message: string
4+
5+
constructor(message: string) {
6+
this.message = message
7+
}
8+
}

lib/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Credentials } from './credentials'
2+
import { LoggingService } from './loggingservice'
3+
export { Credentials, LoggingService }

lib/loggingservice.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Logging Service
2+
3+
import fetch from 'node-fetch';
4+
5+
const LS_QUERY_BASE_URL: string = "https://api.us.paloaltonetworks.com/logging-service/v1/queries"
6+
7+
export class LoggingService {
8+
9+
private url: string;
10+
private auth_token: string;
11+
12+
// Initialize
13+
constructor(url?: string, auth_token?: string) {
14+
this.url = url ? url : LS_QUERY_BASE_URL
15+
this.auth_token = auth_token ? auth_token : undefined
16+
17+
};
18+
19+
public set_auth_token(auth_token: string) { this.auth_token = auth_token };
20+
public get_auth_token(): string { return this.auth_token };
21+
22+
public async create_query(startTime: number, endTime: number, maxWaitTime: number, sql: string):Promise <any> {
23+
let url: string = this.url; // .bind(this)); // TODO: is bind(this) a better way to do it?
24+
let auth_token = this.auth_token
25+
let res = await fetch(url, {
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
'Authorization': 'Bearer ' + auth_token
30+
},
31+
body: JSON.stringify({
32+
"startTime": startTime,
33+
"endTime": endTime,
34+
"maxWaitTime": maxWaitTime,
35+
"query": sql
36+
}),
37+
});
38+
39+
if (res.ok === false)
40+
throw(`PanCloudError() ${res.status} ${res.statusText}`)
41+
42+
try {
43+
let r_json = await res.json()
44+
return r_json
45+
} catch (exception) {
46+
throw (`PanCloudError() Invalid JSON: ${exception}`)
47+
}
48+
};
49+
50+
public async poll(queryId: string, index: number): Promise <any> {
51+
let url: string = `${this.url}/${queryId}/${index}`
52+
let auth_token = this.auth_token
53+
console.log('poll(): url is: ', url)
54+
console.log('poll(): queryId is :', queryId)
55+
let res = await fetch(url, {
56+
method: 'GET',
57+
headers: {
58+
'Content-Type': 'application/json',
59+
'Authorization': 'Bearer ' + auth_token
60+
}
61+
})
62+
if (res.ok === false)
63+
throw(`PanCloudError() ${res.status} ${res.statusText}`)
64+
65+
try {
66+
let r_json = await res.json()
67+
return r_json
68+
} catch (exception) {
69+
throw (`PanCloudError() Invalid JSON: ${exception}`)
70+
}
71+
}
72+
}

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "pancloud-nodejs",
3+
"version": "1.0.0",
4+
"description": "Palo Alto Networks Application Framework NodeJS SDK",
5+
"main": "index.js",
6+
"directories": {
7+
"lib": "lib"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/PaloAltoNetworks/pancloud-nodejs.git"
15+
},
16+
"keywords": [
17+
"pancloud",
18+
"nodejs",
19+
"applicationframework"
20+
],
21+
"author": "Palo Alto Networks",
22+
"license": "ISC",
23+
"bugs": {
24+
"url": "https://github.com/PaloAltoNetworks/pancloud-nodejs/issues"
25+
},
26+
"homepage": "https://github.com/PaloAltoNetworks/pancloud-nodejs#readme"
27+
}

0 commit comments

Comments
 (0)