-
Notifications
You must be signed in to change notification settings - Fork 10
/
utilities.go
85 lines (72 loc) · 2.58 KB
/
utilities.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
// utilities.go
// For utility/helper functions to support the jamf pro tf provider
package utils
import (
"encoding/base64"
"fmt"
"os"
"strings"
)
// ConvertToXMLSafeString replaces disallowed XML characters in a string with their corresponding XML entity references.
// This function is useful for preparing a string to be safely included in an XML document.
func ConvertToXMLSafeString(s string) string {
// Define a map of disallowed characters and their XML entity equivalents.
replacements := map[string]string{
"&": "&",
"<": "<",
">": ">",
"'": "'",
"\"": """,
}
// Replace each disallowed character with its entity reference.
for key, val := range replacements {
s = strings.ReplaceAll(s, key, val)
}
// Return the XML-safe string.
return s
}
// ConvertFromXMLSafeString reverses the process of ConvertToXMLSafeString.
// It replaces XML entity references in a string back to their original characters.
// This is useful when reading XML data that contains entity references and converting them back to normal characters.
func ConvertFromXMLSafeString(s string) string {
// Define a map of XML entities and their corresponding characters.
replacements := map[string]string{
"&": "&",
"<": "<",
">": ">",
"'": "'",
""": "\"",
}
// Replace each entity reference with its corresponding character.
for key, val := range replacements {
s = strings.ReplaceAll(s, key, val)
}
// Return the original string with characters restored.
return s
}
func Base64EncodeCertificate(certPath string) (string, error) {
// Read the certificate file
data, err := os.ReadFile(certPath)
if err != nil {
return "", fmt.Errorf("failed to read certificate file: %v", err)
}
// Base64 encode the file's content
encoded := base64.StdEncoding.EncodeToString(data)
return encoded, nil
}
// EnsureXMLSafeString checks if a string contains disallowed XML characters.
// If it does, it converts the string to an XML-safe format using ConvertToXMLSafeString.
// This function is useful for ensuring that strings are safe for inclusion in XML documents.
func EnsureXMLSafeString(s string) string {
// Define a set of disallowed XML characters.
disallowedChars := []string{"&", "<", ">", "'", "\""}
// Check if the string contains any disallowed characters.
for _, char := range disallowedChars {
if strings.Contains(s, char) {
// If a disallowed character is found, convert the entire string to an XML-safe format.
return ConvertToXMLSafeString(s)
}
}
// If no disallowed characters are found, return the original string.
return s
}