-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
290 lines (251 loc) · 7.22 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/jackc/pgx/v5"
flag "github.com/spf13/pflag"
)
// Used for storing the results from the database.
type UserEntity struct {
ID string
Email sql.NullString
FirstName sql.NullString
LastName sql.NullString
RealmName sql.NullString
Username string
CreatedTimestamp int64
}
const (
DB_USERNAME = "keycloak"
DB_PASSWORD = "password"
DB_HOST = "localhost"
DB_NAME = "keycloak"
DB_PORT = 5432
MAX_AGE_IN_DAYS = 30
EMPTY_DAYS = -1
KC_REALM = "master"
)
// Environment constants
const (
ENV_CLIENT_REALM = "KC_CLIENT_REALM"
ENV_USERNAME = "KC_DB_USERNAME"
ENV_PASSWORD = "KC_DB_PASSWORD"
// Deletion on days.
ENV_MAX_AGE_IN_DATE = "KC_MAX_AGE_IN_DATE"
ENV_MAX_AGE_IN_DAYS = "KC_MAX_AGE_IN_DAYS"
)
var (
username string
password string
realm string
dbName string
dbHost string
dbPort int
maxAgeInDays int
deleteDate string
incUsername bool
incEmail bool
incId bool
incFirstName bool
incLastName bool
incCreatedTimestamp bool
)
func main() {
// Get the path to the executable file
exePath, err := os.Executable()
if err != nil {
fmt.Println("[M] Error:", err)
return
}
// Get the name of the executable file
exeName := filepath.Base(exePath)
// Parse command-line arguments
flag.NewFlagSet("Find Disconnected IDP Users in Keycloak", flag.ExitOnError)
flag.StringVarP(&username, "username", "U", DB_USERNAME, "Database username.")
flag.StringVarP(&password, "password", "W", DB_PASSWORD, "Database password.")
flag.StringVarP(&dbName, "dbname", "d", DB_NAME, "Database Name.")
flag.StringVarP(&dbHost, "host", "h", DB_HOST, "Specifies the host name of the machine on which the server is running.")
flag.IntVarP(&dbPort, "port", "p", DB_PORT, "Database Port.")
flag.StringVarP(&realm, "realm", "r", KC_REALM, "Keycloak Realm")
flag.IntVar(&maxAgeInDays, "days", EMPTY_DAYS, "the number of days, after which users are deleted")
flag.StringVar(&deleteDate, "deleteDate", "", "The date after which users will be deleted. Format: YYYY-MM-DD")
flag.BoolVarP(&incId, "incId", "0", false, "Include id in the output")
flag.BoolVarP(&incUsername, "incUsername", "1", true, "Include username in the output")
flag.BoolVarP(&incEmail, "incEmail", "2", false, "Include email in the output")
flag.BoolVarP(&incFirstName, "incFirstName", "3", false, "Include first name in the output")
flag.BoolVarP(&incLastName, "incLastName", "4", false, "Include last name in the output")
flag.BoolVarP(&incCreatedTimestamp, "incCreatedTimestamp", "5", false, "Include created timestamp in the output")
flag.CommandLine.SortFlags = false
flag.Parse()
log.Printf("[START] %s", exeName)
//urlExample := "postgres://username:password@localhost:5432/database_name"
dbUrl := fmt.Sprintf("postgres://%s:%s@%s:%d/%s",
username,
password,
dbHost, dbPort, dbName)
conn, err := pgx.Connect(context.Background(), dbUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(context.Background())
// get the offset for searhing in the db.
//var offset int64 = 0
maxAge, err := generateEpoch(maxAgeInDays, deleteDate)
if err != nil {
fmt.Println("[M] Error:", err)
return
}
log.Println("[INFO] maxAge:", maxAge, " date:", keycloakEpochToDate(maxAge))
// Create the SQL Query to run against the keycloak database
sqlQuery := fmt.Sprintf("select ue.id, ue.email, ue.first_name, ue.last_name,r.name as realm_name, ue.username, ue.created_timestamp FROM user_entity as UE left join REALM as R on (ue.realm_id = r.id) where r.name = '%s' AND ue.created_timestamp <= %d;", realm, maxAge)
rows, err := conn.Query(context.Background(), sqlQuery)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var userEntities []UserEntity
var count int = 0
for rows.Next() {
count++
var userEntity UserEntity
err := rows.Scan(
&userEntity.ID,
&userEntity.Email,
&userEntity.FirstName,
&userEntity.LastName,
&userEntity.RealmName,
&userEntity.Username,
&userEntity.CreatedTimestamp,
)
if err != nil {
log.Fatal(err)
}
userEntities = append(userEntities, userEntity)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
if count > 0 {
fmt.Println()
}
var newLn bool = false
// Print the parsed objects
for _, userEntity := range userEntities {
if incId {
fmt.Printf("%s", userEntity.ID)
newLn = true
}
if incUsername {
if newLn {
fmt.Printf(",")
}
fmt.Printf("%s", userEntity.Username)
newLn = true
}
if incEmail {
if newLn {
fmt.Printf(",")
}
fmt.Printf("%s", userEntity.Email.String)
newLn = true
}
if incFirstName {
if newLn {
fmt.Printf(",")
}
fmt.Printf("%s", userEntity.FirstName.String)
newLn = true
}
if incLastName {
if newLn {
fmt.Printf(",")
}
fmt.Printf("%s", userEntity.LastName.String)
newLn = true
}
if incCreatedTimestamp {
if newLn {
fmt.Printf(",")
}
fmt.Printf("%d", userEntity.CreatedTimestamp)
newLn = true
}
if newLn {
fmt.Printf("\n")
newLn = false
}
}
fmt.Println()
log.Printf("dbhost=%s dbname=%s realm=%s %s=%d\n", dbHost, dbName, realm, "rows", count)
log.Printf("[END] %s", exeName)
}
func parseEnvVariables() {
envUsername := os.Getenv(ENV_USERNAME)
if envUsername != "" {
username = envUsername
}
envPassword := os.Getenv(ENV_PASSWORD)
if envPassword != "" {
password = envPassword
}
envClientRealm := os.Getenv(ENV_CLIENT_REALM)
if envClientRealm != "" {
realm = envClientRealm
}
var err error
envDays := os.Getenv(ENV_MAX_AGE_IN_DAYS)
if envDays != "" {
maxAgeInDays, err = strconv.Atoi(envDays)
if err != nil {
log.Fatal("Error parsing days from env variable: ", err)
panic("Error parsing threads from env variable: " + ENV_MAX_AGE_IN_DAYS + err.Error())
}
}
logCmdLineArgs()
}
// Send the command line arguments to the log file
func logCmdLineArgs() {
log.Println("[KeyCloak Delete via DB Tool (Day/Date Based)]")
log.Println(" Authentication:")
log.Println(" username:", username)
log.Println(" password:", password)
log.Println(" host:", dbHost)
log.Println(" port:", dbPort)
log.Println(" database:", dbName)
log.Println(" Deletion Criteria")
if maxAgeInDays > EMPTY_DAYS {
log.Println(" maxDaysInAge:", maxAgeInDays, "[", daysToKCEpoch(maxAgeInDays), "]")
} else {
log.Println(" maxDaysInAge:", "disabled")
}
if deleteDate != "" {
log.Println(" deleteDate:", deleteDate)
} else {
log.Println(" deleteDate:", "Disabled")
}
log.Println(" ")
}
// Convert days to a Keycloak Epoch value
func daysToKCEpoch(days int) int64 {
return int64(days * 86400)
}
func generateEpoch(days int, date string) (int64, error) {
if date != "" {
t, err := time.Parse("2006-01-02", date)
if err != nil {
return 0, err
}
return t.Unix() * 1000, nil
}
return time.Now().AddDate(0, 0, -days).Unix() * 1000, nil
}
func keycloakEpochToDate(epoch int64) string {
return time.Unix(epoch/1000, 0).Format("2006-01-02")
}