2022-11-08 09:54:51 +00:00
|
|
|
from io import StringIO
|
|
|
|
|
2021-07-23 08:41:02 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
from common.utils import ssh_private_key_gen, validate_ssh_private_key
|
|
|
|
|
2021-07-23 08:41:02 +00:00
|
|
|
|
2022-06-14 10:04:53 +00:00
|
|
|
def validate_password_for_ansible(password):
|
|
|
|
""" 校验 Ansible 不支持的特殊字符 """
|
2021-07-23 08:41:02 +00:00
|
|
|
# validate password contains left double curly bracket
|
|
|
|
# check password not contains `{{`
|
2022-06-14 10:04:53 +00:00
|
|
|
# Ansible 推送的时候不支持
|
2021-07-23 08:41:02 +00:00
|
|
|
if '{{' in password:
|
|
|
|
raise serializers.ValidationError(_('Password can not contains `{{` '))
|
2022-06-14 10:04:53 +00:00
|
|
|
# Ansible Windows 推送的时候不支持
|
|
|
|
if "'" in password:
|
|
|
|
raise serializers.ValidationError(_("Password can not contains `'` "))
|
|
|
|
if '"' in password:
|
|
|
|
raise serializers.ValidationError(_('Password can not contains `"` '))
|
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
def validate_ssh_key(ssh_key, passphrase=None):
|
|
|
|
valid = validate_ssh_private_key(ssh_key, password=passphrase)
|
|
|
|
if not valid:
|
|
|
|
raise serializers.ValidationError(_("private key invalid or passphrase error"))
|
|
|
|
|
|
|
|
ssh_key = ssh_private_key_gen(ssh_key, password=passphrase)
|
|
|
|
string_io = StringIO()
|
|
|
|
ssh_key.write_private_key(string_io)
|
|
|
|
ssh_key = string_io.getvalue()
|
|
|
|
return ssh_key
|