/
passwordExpiryWatcher.go
86 lines (72 loc) · 2.03 KB
/
passwordExpiryWatcher.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/TheJumpCloud/jcapi"
)
func main() {
// Input parameters
var apiKey string
var csvFile string
// Obtain the input parameters
flag.StringVar(&csvFile, "output", "", "-output=<filename>")
flag.StringVar(&apiKey, "key", "", "-key=<API-key-value>")
flag.Parse()
if csvFile == "" || apiKey == "" {
fmt.Println("Usage of ./CSVImporter:")
fmt.Println(" -output=\"\": -output=<filename>")
fmt.Println(" -key=\"\": -key=<API-key-value>")
return
}
// Attach to JumpCloud
jc := jcapi.NewJCAPI(apiKey, jcapi.StdUrlBase)
// Fetch all users who's password expires between given dates in
userList, err := jc.GetSystemUsers(false)
if err != nil {
fmt.Printf("Could not read system users, err='%s'\n", err)
return
}
// Setup access the CSV file specified
path, err := filepath.Abs(csvFile)
if err != nil {
log.Fatal("Entered an incorrect file path for CSV output")
}
file, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
w := csv.NewWriter(file)
if err := w.Write([]string{"FIRSTNAME", "LASTNAME", "EMAIL", "PASSWORD EXPIRY DATE", "PASSWORD EXPIRED", "MFA ENABLED", "MFA VERIFIED"}); err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, record := range userList {
nullTime := time.Time{}
var expired, passwordExpirationString string
if record.PasswordExpired {
expired = "YES"
} else {
expired = "NO"
}
if record.PasswordExpirationDate == nullTime {
passwordExpirationString = "No Date Set"
} else {
passwordExpirationString = record.PasswordExpirationDate.String()
}
if err := w.Write([]string{record.FirstName, record.LastName, record.Email, passwordExpirationString, expired, strconv.FormatBool(record.EnableUserPortalMultifactor), strconv.FormatBool(record.TotpEnabled)}); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
fmt.Println("Finished")
return
}