This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdd-GitLabToken.ps1
131 lines (119 loc) · 3.57 KB
/
Add-GitLabToken.ps1
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
Function Add-GitLabToken
{
<#
.SYNOPSIS
Adds a access token to the local GitLab-API instance
.DESCRIPTION
Adds a access token to the local GitLab-API instance.
First checks if the specified token and hostname is valid.
If valid adds it to the local configuration file.
If -Active is used the token will be added as the active token.
.EXAMPLE
Add-GitLabToken -GitLabURI gitlab.com -Token XXXXXXXXXX
---------------------------------------------------------------
Adds a GitlabToken for gitlab.com to the local instance of the powershell gitlab-api
#>
[CmdletBinding()]
[Alias()]
[OutputType()]
param(
#Gitlab URI, e.a. https://gitab.com
[Parameter(HelpMessage = 'GitlabServer URI',
Mandatory = $true)]
[ValidatePattern("^(?:http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$")]
[string]$GitLabURI,
#Token supplied from gitlab. This can be an private and an Access Token.
[Parameter(HelpMessage = 'access token GitlabServer',
Mandatory = $true)]
[string]$Token,
#If specified adds the token as the active token
[Parameter(HelpMessage = 'is token active')]
[Switch]$active
)
#region check token
$header = @{
'PRIVATE-TOKEN' = $Token
}
$userurl = "$GitLabURI/api/v3/user"
try
{
$result = Invoke-RestMethod -Uri $userurl -Headers $header
$gitlabusername = $result.username
$errorprop = $null
}
catch [System.Net.WebException]
{
switch -Wildcard ($_.exception.Message)
{
'*Could not create SSL/TLS secure channel.*'
{
$errorprop = @{
message = "could not reach server at $userurl"
category = 'ConnectionError'
}
}
'*401*'
{
$errorprop = @{
message = "(401)token not valid for server $userurl"
category = 'AuthenticationError'
}
}
'*500*'
{
$errorprop = @{
message = "(500)Server error at $userurl"
category = 'AuthenticationError'
}
}
}
}
catch
{
{
$errorprop = @{
message = $_.exception.message
category = $_.categoryinfo.category
}
}
}
finally
{
if($errorprop)
{
Write-Error @errorprop -ErrorAction Stop
}
}
#endregion
#region check for existing token
$Keyitem = Import-Clixml $script:GitlabKeyfile
foreach($key in $Keyitem.keys)
{
if(
($key.gitlabhost -eq $GitLabURI) -and
($key.gitlabuser.username -eq $gitlabusername)
)
{
$errormessage = "Combination of $GitLabURI and $gitlabusername (user associated with passed token) already exists. remove token before creation."
Write-Error $errormessage -Category ResourceExists -ErrorAction Stop
}
}
#endregion
#region create new tokenobj
$securekey = ConvertTo-SecureString -String $Token -AsPlainText -Force
$gitlabuser = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $gitlabusername, $securekey
$newkey = [pscustomobject]@{
ID = [guid]::NewGuid().ToString()
GitLabHost = $GitLabURI
GitLabUser = $gitlabuser
}
#endregion
#region save to config
$Keyitem.keys += $newkey
if(($Keyitem.keys.count -eq 1)-or $active)
{
$Keyitem.Activekey = $newkey.id
}
Export-Clixml -InputObject $Keyitem -Path $script:GitlabKeyfile
#endregion
}