MCPcopy Create free account
hub / github.com/bytebase/bytebase / readTLSPathFile

Function readTLSPathFile

backend/plugin/db/util/ssl.go:64–99  ·  view source on GitHub ↗
(label, path string)

Source from the content-addressed store, hash-verified

62}
63
64func readTLSPathFile(label, path string) (string, error) {
65 if !filepath.IsAbs(path) {
66 return "", errors.Errorf("%s path must be absolute", label)
67 }
68 info, err := os.Stat(path)
69 if err != nil {
70 return "", errors.Errorf("failed to read %s file", label)
71 }
72 if !info.Mode().IsRegular() {
73 return "", errors.Errorf("%s path must point to a regular file", label)
74 }
75 if info.Size() == 0 {
76 return "", errors.Errorf("%s file is empty", label)
77 }
78 if info.Size() > maxTLSMaterialFileSize {
79 return "", errors.Errorf("%s file is too large", label)
80 }
81
82 file, err := os.Open(path)
83 if err != nil {
84 return "", errors.Errorf("failed to read %s file", label)
85 }
86 defer file.Close()
87
88 content, err := io.ReadAll(io.LimitReader(file, maxTLSMaterialFileSize+1))
89 if err != nil {
90 return "", errors.Errorf("failed to read %s file", label)
91 }
92 if len(content) == 0 {
93 return "", errors.Errorf("%s file is empty", label)
94 }
95 if int64(len(content)) > maxTLSMaterialFileSize {
96 return "", errors.Errorf("%s file is too large", label)
97 }
98 return string(content), nil
99}
100
101// GetTLSConfig gets the TLS config for connection.
102// The datasource must already have path-backed TLS material resolved via ResolveTLSMaterial.

Callers 1

ResolveTLSMaterialFunction · 0.85

Calls 3

ErrorfMethod · 0.80
OpenMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected