-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
186 lines (156 loc) · 6.2 KB
/
main.go
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
183
184
185
186
// Acceldata Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
//
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
netURL "net/url"
"os"
"strings"
"github.com/integrii/flaggy"
)
// Configurations
// NOTE: All these below configutaions are global scoped
// DO NOT MUTATE them any where in the program
var (
Version = "0.0.0"
BuildID = "0"
url = ""
reqType = "GET"
isKerberized = false
keytabPath = "/etc/security/hdfs-headless.keytab"
kerberosPrinciple = "hdfs@ACME.ORG"
timestampLayout = "02/01/2006"
isBasicAuth = ""
basicAuthUser = ""
basicAuthPassword = ""
outputFile = ""
clientUserAgent = "gurl/0.0.1"
enforceTLSVerify = false
reqHTTPMethod httpMethod
availableTimestampLayouts = []string{"01/02/2006", "01/02/06", "02/01/2006", "02/01/06", "2006/01/02", "06/01/02", "2006/02/01", "06/02/01"}
defaultShell = "/usr/bin/sh"
defaultKRBConfig = "/etc/krb5.conf"
secondaryKRBConfig = "/etc/krb5/krb5.conf"
)
func init() {
flaggy.SetName("gURL")
flaggy.SetDescription("gURL - A replacement for statically compiled cURL binary")
flaggy.DefaultParser.ShowHelpOnUnexpected = true
flaggy.DefaultParser.AdditionalHelpAppend = `
Usage Format: "gurl -X HTTP_REQ_TYPE -ua user-agent -u "basic-auth-username:basic-auth-password" -k isKerberosEnabled -kt kerberos-keytab-path -kp kerberos-principle -ts timestamp-layout -ev enforce-TLS-verification -l url"
Example: "gurl -X GET -ua "gurl/0.0.1" -u "hdfs:" -k -kt /etc/security/hdfs-headless.keytab -kp hdfs@ACME.ORG -ts '01/02/2006' -ev -l https://node01.acme.org:9871/"`
flaggy.DefaultParser.AdditionalHelpPrepend = "https://acceldata.io/"
// set the version and parse all inputs into variables
version := Version + "\n" + "Build ID: " + BuildID
flaggy.SetVersion(version)
//
flaggy.String(&url, "l", "url", "URL to make request")
flaggy.String(&reqType, "X", "type", "HTTP request type to use")
flaggy.Bool(&isKerberized, "k", "kerberized", "Is Kerberos enabled for the URL")
flaggy.String(&keytabPath, "kt", "keytab-path", "Kerberos Keytab Path")
flaggy.String(&kerberosPrinciple, "kp", "kerberos-principle", "Kerberos principle to use with keytab")
flaggy.String(×tampLayout, "ts", "ts-format", "Timestamp format klist uses in 'Go Time Format'. Example: 'mm/dd/yyyy' => '01/02/2006'")
flaggy.String(&isBasicAuth, "u", "basic-auth", "Is Basic Auth Enabled for the URL")
flaggy.Bool(&enforceTLSVerify, "ev", "enforce-tls-verify", "Enforce TLS certification verification")
flaggy.String(&clientUserAgent, "ua", "user-agent", "User Agent to be set for the client requests")
flaggy.String(&outputFile, "o", "output-file", "Write the request response to a file")
flaggy.Parse()
// Trim Extra Space from all user inputs
url = strings.TrimSpace(url)
reqType = strings.TrimSpace(reqType)
clientUserAgent = strings.TrimSpace(clientUserAgent)
isBasicAuth = strings.TrimSpace(isBasicAuth)
// Args validation & manipulation
if url == "" {
flaggy.ShowHelpAndExit("ERROR: 'url' parameter is required")
} else {
_, err := netURL.Parse(url)
if err != nil {
fmt.Println("ERROR: ", err.Error())
flaggy.ShowHelpAndExit("ERROR: 'url' parameter has a invalid url")
}
}
if isKerberized {
//
keytabPath = strings.TrimSpace(keytabPath)
kerberosPrinciple = strings.TrimSpace(kerberosPrinciple)
timestampLayout = strings.TrimSpace(timestampLayout)
if keytabPath == "" {
flaggy.ShowHelpAndExit("ERROR: 'keytab-path' parameter is required")
}
if _, err := os.Stat(keytabPath); err != nil {
flaggy.ShowHelpAndExit("ERROR: cannot find or access the keytab file '" + keytabPath + "' because " + err.Error())
}
if kerberosPrinciple == "" {
flaggy.ShowHelpAndExit("ERROR: 'kerberos-principle' parameter is required")
}
if timestampLayout != "" {
if !isInSlice(timestampLayout, availableTimestampLayouts) {
fmt.Printf("WARN: '%s' is not in the default 'ts-format' values\n", timestampLayout)
} else {
availableTimestampLayouts = removeFromSlice(timestampLayout, availableTimestampLayouts)
}
}
// Check the dependecies
if err := isKRBDepsAvail(); err != nil {
flaggy.ShowHelpAndExit("ERROR: " + err.Error())
}
}
// Basic Auth
if isBasicAuth != "" {
//
basicAuthCreds := strings.Split(isBasicAuth, ":")
basicAuthUser = strings.TrimSpace(basicAuthCreds[0])
if len(basicAuthCreds) >= 2 {
basicAuthPassword = strings.TrimSpace(strings.Join(basicAuthCreds[1:], ""))
}
}
}
func main() {
// ------- NOTE ---------------
// If kerberos is enabled
// We expect 'kinit', 'klist', 'awk', 'grep' & 'head' binaries in the OS path
// We expect '/etc/krb5.conf' file to be present
// If the 'krb5.conf' path is different set it @ env 'KRB5_CONFIG'
//
// The temporary token will be generated @ '/tmp/krb5cc_<CURRENT-LINUX-UID>'
// Incase of different location set it @ env 'KRB5CCNAME'
// ------- NOTE ---------------
// Check if kerberos is enabled
if isKerberized {
isKerberosCacheValid, err := isKerberosCacheValid(timestampLayout)
if err != nil {
fmt.Println("ERROR: Unable to validate Kerberos cache. Because: ", err.Error())
os.Exit(1)
}
if !isKerberosCacheValid {
if err := doKinit(keytabPath, kerberosPrinciple); err != nil {
fmt.Println("ERROR: Unable to do Kinit. Because: ", err.Error())
os.Exit(1)
}
}
}
if reqHTTPMethod, err := stringToMethod(reqType); err == nil {
_, n, err := makeRequest(reqHTTPMethod, url)
if err != nil {
fmt.Println("STATUS: ", n)
fmt.Println("ERROR: ", err)
os.Exit(1)
}
} else {
fmt.Println("ERROR:", err.Error())
os.Exit(1)
}
}