Fix bug 1467
[osm/N2VC.git] / n2vc / utils.py
index e8cf64d..16a4733 100644 (file)
 #     See the License for the specific language governing permissions and
 #     limitations under the License.
 
+import base64
+import re
+import binascii
 from enum import Enum
 from juju.machine import Machine
 from juju.application import Application
 from juju.action import Action
 from juju.unit import Unit
+from n2vc.exceptions import N2VCInvalidCertificate
+
+
+def base64_to_cacert(b64string):
+    """Convert the base64-encoded string containing the VCA CACERT.
+
+    The input string....
+
+    """
+    try:
+        cacert = base64.b64decode(b64string).decode("utf-8")
+
+        cacert = re.sub(r"\\n", r"\n", cacert,)
+    except binascii.Error as e:
+        raise N2VCInvalidCertificate(message="Invalid CA Certificate: {}".format(e))
+
+    return cacert
 
 
 class N2VCDeploymentStatus(Enum):