-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMavenCliProxy.ts
More file actions
182 lines (148 loc) · 5.91 KB
/
MavenCliProxy.ts
File metadata and controls
182 lines (148 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*!
* Copyright 2018-2019 VMware, Inc.
* SPDX-License-Identifier: MIT
*/
import * as path from "path"
import * as fs from "fs-extra"
import * as jwtDecode from "jwt-decode"
import { BaseEnvironment } from "../platform"
import { MavenInfo } from "../types"
import { Logger, proc } from ".."
const archetypeIdByProjectType: { [key: string]: string } = {
"vro-ts": "package-typescript-archetype",
"vro-js": "package-actions-archetype",
"vro-xml": "package-xml-archetype",
"vro-mixed": "package-mixed-archetype",
"vra-yaml": "package-vra-archetype",
"vra-vro": "package-vrealize-archetype",
"vra-ng": "package-vra-ng-archetype"
}
export class MavenCliProxy {
constructor(private environment: BaseEnvironment, private mavenSettings: MavenInfo, private logger: Logger) {
logger.debug(`Initializing Maven CLI proxy for profile '${mavenSettings.profile}'`)
}
async getToken(): Promise<string> {
if (!this.mavenSettings.profile) {
throw new Error("Cannot retrieve token because of missing value for 'vrdev.maven.profile' setting")
}
const tokenFile = this.environment.getGlobalTokenFile()
const tokenFolder = path.dirname(this.environment.getGlobalTokenFile())
const tokenPom = path.join(tokenFolder, "pom.xml")
this.writeTokenPom(tokenPom)
let token = fs.existsSync(tokenFile) ? this.readTokenFile(tokenFile) : null
if (!token || this.isExpired(token) || this.isDiffUserOrTenant(token)) {
const command = `mvn vrealize:auth -P${this.mavenSettings.profile} -DoutputDir="${tokenFolder}" -N -e`
const cmdOptions = { cwd: tokenFolder }
await proc.exec(command, cmdOptions, this.logger)
token = this.readTokenFile(tokenFile)
}
return token.value
}
createProject(
projectType: string,
groupId: string,
artifactId: string,
destinationDir: string,
requiresWorkflows: boolean,
workflowsPath?: string
): Promise<proc.CmdResult> {
const archetypeId = archetypeIdByProjectType[projectType]
if (!archetypeId) {
return Promise.reject(`Unsupported project type: ${projectType}`)
}
let archetypeGroup = "o11n"
if (projectType === "vra-yaml") {
archetypeGroup = "vra"
} else if (projectType === "vra-ng") {
archetypeGroup = "vra-ng"
}
let command =
`mvn archetype:generate -DinteractiveMode=false ` +
`-DarchetypeGroupId=com.vmware.pscoe.${archetypeGroup}.archetypes ` +
`-DarchetypeArtifactId=${archetypeId} ` +
`-DarchetypeVersion=${this.environment.buildToolsVersion} ` +
`-DgroupId=${groupId} ` +
`-DartifactId=${artifactId}`
if (requiresWorkflows) {
if (!workflowsPath) {
return Promise.reject(`Project type ${projectType} requires a workflows directory`)
}
command += ` -DworkflowsPath="${workflowsPath}"`
}
return proc.exec(command, { cwd: destinationDir }, this.logger)
}
copyDependency(
groupId: string,
artifactId: string,
version: string,
packaging: string,
destinationDir: string
): Promise<proc.CmdResult> {
const command =
`mvn dependency:copy ` +
`-Dartifact=${groupId}:${artifactId}:${version}:${packaging} ` +
`-DoutputDirectory="${destinationDir}" ` +
`-Dmdep.stripVersion=true `
return proc.exec(command, { cwd: destinationDir }, this.logger)
}
private readTokenFile(filePath: string): { value: string; expirationDate: string } {
const content = fs.readFileSync(filePath, { encoding: "utf8" })
const token = JSON.parse(content)
if (!token || !token.value || !token.expirationDate) {
throw new Error(`Missing or invalid token file: ${filePath}`)
}
return token
}
private writeTokenPom(filePath: string): void {
const content = `<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.vmware.pscoe.o11n</groupId>
<artifactId>token-pom</artifactId>
<version>1</version>
<packaging>pom</packaging>
<parent>
<groupId>com.vmware.pscoe.o11n</groupId>
<artifactId>base-package</artifactId>
<version>${this.environment.buildToolsVersion}</version>
</parent>
</project>`
fs.writeFileSync(filePath, content)
}
private isExpired(token: { value: string; expirationDate: string }): boolean {
const expirationDate = Date.parse(token.expirationDate)
const now = Date.now()
return now > expirationDate
}
private isDiffUserOrTenant(token: { value: string; expirationDate: string }): boolean {
let decodedToken
try {
decodedToken = jwtDecode(token.value)
} catch (e) {
this.logger.warn(`Invalid local SSO authentication token format!`)
return true;
}
// token (stored locally) details
const tokenUserQualifier = decodedToken.prn // user@TENANT
if (!tokenUserQualifier) {
return true;
}
const tokenUsername = tokenUserQualifier.match(/.+?(?=@)/)
if (!tokenUsername) {
return true;
}
const tokenTenant = tokenUserQualifier.match(/(?<=@).+[^\s]/)
if (!tokenTenant) {
return true;
}
const tokenDomain = decodedToken.domain
if (!tokenDomain) {
return true;
}
// Maven active profile details
const vroUsername = this.environment.getVroUsername() // user@domain
const vroTenant = this.environment.getVroTenant()
return (`${tokenUsername[0]}@${tokenDomain}`.toUpperCase() != vroUsername.toUpperCase() ||
tokenTenant[0].toUpperCase() != vroTenant.toUpperCase());
}
}