-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from k8spacket/tls-certificates
Show server TLS certificate chain
- Loading branch information
Showing
8 changed files
with
233 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package db | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/HouzuoGuo/tiedot/db" | ||
"github.com/fatih/structs" | ||
"github.com/k8spacket/plugins/tls-parser/metrics/model" | ||
) | ||
|
||
const connectionColName = "TLSConnections" | ||
const detailsColName = "TLSDetails" | ||
|
||
var connectionsCol, detailsCol = buildDatabase() | ||
|
||
func buildDatabase() (*db.Col, *db.Col) { | ||
|
||
dbDir := "./Database" | ||
|
||
database, err := db.OpenDB(dbDir) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if database.ColExists(connectionColName) == false { | ||
if err := database.Create(connectionColName); err != nil { | ||
panic(err) | ||
} | ||
if err := database.Use(connectionColName).Index([]string{"id"}); err != nil { | ||
panic(err) | ||
} | ||
} | ||
if database.ColExists(detailsColName) == false { | ||
if err := database.Create(detailsColName); err != nil { | ||
panic(err) | ||
} | ||
if err := database.Use(detailsColName).Index([]string{"id"}); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
return database.Use(connectionColName), database.Use(detailsColName) | ||
} | ||
|
||
func Insert[T metrics.TLSDetails | metrics.TLSConnection](id int, document T) { | ||
var col = getCol(document) | ||
var doc, _ = col.Read(id) | ||
if len(doc) > 0 { | ||
_ = col.Update(id, structs.Map(document)) | ||
} else { | ||
_ = col.InsertRecovery(id, structs.Map(document)) | ||
} | ||
} | ||
|
||
func Read[T metrics.TLSDetails | metrics.TLSConnection](docId int, s T) T { | ||
var document, _ = getCol(s).Read(docId) | ||
var jsonBytes, _ = json.Marshal(document) | ||
_ = json.Unmarshal(jsonBytes, &s) | ||
return s | ||
} | ||
|
||
func ReadAll[T metrics.TLSDetails | metrics.TLSConnection](s T) []T { | ||
col := getCol(s) | ||
|
||
var query interface{} | ||
err := json.Unmarshal([]byte(`["all"]`), &query) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
queryResult := make(map[int]struct{}) | ||
if err := db.EvalQuery(query, col, &queryResult); err != nil { | ||
panic(err) | ||
} | ||
|
||
var result []T | ||
for id := range queryResult { | ||
result = append(result, Read(id, s)) | ||
} | ||
return result | ||
} | ||
|
||
func getCol[T metrics.TLSDetails | metrics.TLSConnection](s T) *db.Col { | ||
switch any(s).(type) { | ||
case metrics.TLSConnection: | ||
return connectionsCol | ||
case metrics.TLSDetails: | ||
return detailsCol | ||
} | ||
return nil | ||
} |
Oops, something went wrong.