-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsecurityUtils.go
98 lines (85 loc) · 1.97 KB
/
securityUtils.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
package main
import (
"fmt"
log "github.com/platinasystems/go-common/logs"
"github.com/platinasystems/pcc-blackbox/models"
"os"
"testing"
"time"
pcc "github.com/platinasystems/pcc-blackbox/lib"
"github.com/platinasystems/test"
)
func CreateFileAndUpload(fileName string, key string, fileType string, keyId uint64) (err error) {
var f *os.File
f, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
err = fmt.Errorf("Unable to create file:%v", err)
return
}
defer f.Close()
_, err = f.Write([]byte(key))
if err != nil {
err = fmt.Errorf("Unable to write on disk:%v", err)
return
}
filePath := fmt.Sprintf("./%s", fileName)
// check if exist and delete if so
switch fileType {
case pcc.PRIVATE_KEY, pcc.PUBLIC_KEY:
var (
exist bool
)
exist, _, err = Pcc.FindSecurityKey(fileName)
if exist {
Pcc.DeleteKey(fileName)
}
_, err = Pcc.UploadKey(filePath, fileName, fileType, "")
if err != nil {
return
}
case pcc.CERT:
var (
cert pcc.Certificate
exist bool
)
exist, cert, err = Pcc.FindCertificate(fileName)
if exist {
Pcc.DeleteCertificate(cert.Id)
}
_, err = Pcc.UploadCert(filePath, fileName, "", keyId)
if err != nil {
return
}
}
return
}
func delAllCerts(t *testing.T) {
test.SkipIfDryRun(t)
res := models.InitTestResult(runID)
defer res.CheckTestAndSave(t, time.Now())
assert := test.Assert{t}
var (
certificates []pcc.Certificate
id uint64
err error
)
certificates, err = Pcc.GetCertificates()
if err != nil {
msg := fmt.Sprintf("Failed to get certificates: %v", err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
assert.FailNow()
}
for _, c := range certificates {
id = c.Id
log.AuctaLogger.Infof("Deleting certificate %v", c.Alias)
err = Pcc.DeleteCertificate(id)
if err != nil {
msg := fmt.Sprintf("Failed to delete Certificate %v: %v",
id, err)
res.SetTestFailure(msg)
log.AuctaLogger.Error(msg)
assert.FailNow()
}
}
}