userdata.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package userdata
  15. import (
  16. "bytes"
  17. "compress/gzip"
  18. "encoding/base64"
  19. "io/ioutil"
  20. "strings"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/cloudinit"
  23. "yunion.io/x/pkg/util/osprofile"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. )
  26. const (
  27. UserdataLimitSize = 64 * 1024 // 64 KB
  28. )
  29. func Encode(userdata string) (string, error) {
  30. var buf bytes.Buffer
  31. gz := gzip.NewWriter(&buf)
  32. if _, err := gz.Write([]byte(userdata)); err != nil {
  33. return "", errors.Wrap(err, "failed to gzip userdata")
  34. }
  35. if err := gz.Close(); err != nil {
  36. return "", errors.Wrap(err, "close gzip")
  37. }
  38. return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
  39. }
  40. func Decode(encodeUserdata string) (string, error) {
  41. gzData, err := base64.StdEncoding.DecodeString(encodeUserdata)
  42. if err != nil {
  43. return "", errors.Wrap(err, "base64 decode gzip data")
  44. }
  45. gr, err := gzip.NewReader(bytes.NewBuffer(gzData))
  46. if err != nil {
  47. return "", errors.Wrap(err, "new reader")
  48. }
  49. defer gr.Close()
  50. data, err := ioutil.ReadAll(gr)
  51. if err != nil {
  52. return "", errors.Wrap(err, "read data")
  53. }
  54. return string(data), nil
  55. }
  56. func ValidateUserdata(data string, osType string) error {
  57. if len(data) == 0 {
  58. return nil
  59. }
  60. _, err := cloudinit.ParseUserData(data)
  61. if err != nil {
  62. if osType == osprofile.OS_TYPE_WINDOWS {
  63. if strings.HasPrefix(data, "[bat]\n") || strings.HasPrefix(data, "[powershell]\n") {
  64. // valid
  65. } else {
  66. return errors.Wrap(httperrors.ErrInputParameter, "invalid windows scripts")
  67. }
  68. } else {
  69. if strings.HasPrefix(data, "#!/bin/sh\n") || strings.HasPrefix(data, "#!/bin/bash\n") || strings.HasPrefix(data, "#!/usr/bin/env bash") {
  70. // valid
  71. } else {
  72. return errors.Wrap(httperrors.ErrInputParameter, "invalid shell scripts")
  73. }
  74. }
  75. }
  76. encodeData, err := Encode(data)
  77. if err != nil {
  78. return errors.Wrapf(httperrors.ErrInputParameter, "Encode data error %s", err)
  79. }
  80. if len(encodeData) > UserdataLimitSize {
  81. return errors.Wrapf(httperrors.ErrInputParameter, "user data size %d large limit %d", len(encodeData), UserdataLimitSize)
  82. }
  83. return nil
  84. }