certified.blob — Blob: Safe PEM File I/O
Blob is a thin wrapper around bytes that tracks whether the data is
secret (private key) or public (certificate), and enforces appropriate
file permissions (0o600 for secrets, 0o644 for public data) on write.
Blob
A convenience wrapper for a blob of bytes, mostly representing PEM-encoded data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
the PEM-encoded data. |
required |
is_secret
|
bool
|
True for private-key material (writes with mode 0o600), False for public data (0o644). |
required |
Source code in certified/blob.py
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
bytes()
Returns the data as a bytes object.
Source code in certified/blob.py
94 95 96 | |
tempfile(dir=None)
Context manager for writing data to a temporary file.
The file is created when you enter the context manager, and automatically deleted when the context manager exits.
Many libraries have annoying APIs which require that certificates be specified as filesystem paths, so even if you have already the data in memory, you have to write it out to disk and then let them read it back in again. If you encounter such a library, you should probably file a bug. But in the mean time, this context manager makes it easy to give them what they want.
Example:
Here's how to get requests to use a CA (see also
<http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification>__)::
ca = certified.CA() with ca.cert_pem.tempfile() as ca_cert_path: requests.get("https://localhost/...", verify=ca_cert_path)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir
|
Optional[str]
|
Passed to |
None
|
Source code in certified/blob.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
write(path, append=False)
Writes the data to the file at the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Pstr
|
The path to write to. |
required |
append
|
bool
|
If False (the default), replace any existing file with the given name. If True, append to any existing file. |
False
|
Source code in certified/blob.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
PublicBlob
Bases: Blob
Source code in certified/blob.py
166 167 168 169 | |
PrivateBlob
Bases: Blob
Source code in certified/blob.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
is_user_only
Source code in certified/blob.py
45 46 47 | |
new_file
Fix the file open() API to create new files securely.
Ref: https://stackoverflow.com/questions/5624359/write-file-with-specific-permissions-in-python @Asclepius
Source code in certified/blob.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |