/
uniqserverid.go
45 lines (39 loc) · 997 Bytes
/
uniqserverid.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/glycerine/ruid"
"github.com/mailgun/pelican-protocol"
)
func FetchOrGenSecretIdForService(path string) string {
if FileExists(path) {
secret, err := FetchSecretIdForService(path)
panicOn(err)
return secret
} else {
return GenSecretIdForService(path)
}
}
func FetchSecretIdForService(path string) (string, error) {
by, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(by), nil
}
func GenSecretIdForService(path string) string {
// generate this once and make it a unique RUID for each server's service.
// usernames generated depend on this, so if you change the service Id,
// you will change the user's account name
myExternalIP := pelican.GetExternalIP()
ruidGen := ruid.NewRuidGen(myExternalIP)
id := ruidGen.Ruid2()
fmt.Printf("SecretIdForService = '%s'\n", id)
f, err := os.Create(path)
panicOn(err)
defer f.Close()
f.WriteString(id)
fmt.Fprintf(f, "\n")
return id
}