common.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package controlapi
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/docker/docker/pkg/plugingetter"
  6. "github.com/docker/libnetwork/ipamapi"
  7. "github.com/docker/swarmkit/api"
  8. "github.com/docker/swarmkit/manager/allocator/networkallocator"
  9. "github.com/docker/swarmkit/manager/state/store"
  10. "google.golang.org/grpc"
  11. "google.golang.org/grpc/codes"
  12. )
  13. var isValidDNSName = regexp.MustCompile(`^[a-zA-Z0-9](?:[-_]*[A-Za-z0-9]+)*$`)
  14. // configs and secrets have different naming requirements from tasks and services
  15. var isValidConfigOrSecretName = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[a-zA-Z0-9-_.]*[a-zA-Z0-9])?$`)
  16. func buildFilters(by func(string) store.By, values []string) store.By {
  17. filters := make([]store.By, 0, len(values))
  18. for _, v := range values {
  19. filters = append(filters, by(v))
  20. }
  21. return store.Or(filters...)
  22. }
  23. func filterContains(match string, candidates []string) bool {
  24. if len(candidates) == 0 {
  25. return true
  26. }
  27. for _, c := range candidates {
  28. if c == match {
  29. return true
  30. }
  31. }
  32. return false
  33. }
  34. func filterContainsPrefix(match string, candidates []string) bool {
  35. if len(candidates) == 0 {
  36. return true
  37. }
  38. for _, c := range candidates {
  39. if strings.HasPrefix(match, c) {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. func filterMatchLabels(match map[string]string, candidates map[string]string) bool {
  46. if len(candidates) == 0 {
  47. return true
  48. }
  49. for k, v := range candidates {
  50. c, ok := match[k]
  51. if !ok {
  52. return false
  53. }
  54. if v != "" && v != c {
  55. return false
  56. }
  57. }
  58. return true
  59. }
  60. func validateAnnotations(m api.Annotations) error {
  61. if m.Name == "" {
  62. return grpc.Errorf(codes.InvalidArgument, "meta: name must be provided")
  63. }
  64. if !isValidDNSName.MatchString(m.Name) {
  65. // if the name doesn't match the regex
  66. return grpc.Errorf(codes.InvalidArgument, "name must be valid as a DNS name component")
  67. }
  68. if len(m.Name) > 63 {
  69. // DNS labels are limited to 63 characters
  70. return grpc.Errorf(codes.InvalidArgument, "name must be 63 characters or fewer")
  71. }
  72. return nil
  73. }
  74. func validateConfigOrSecretAnnotations(m api.Annotations) error {
  75. if m.Name == "" {
  76. return grpc.Errorf(codes.InvalidArgument, "name must be provided")
  77. } else if len(m.Name) > 64 || !isValidConfigOrSecretName.MatchString(m.Name) {
  78. // if the name doesn't match the regex
  79. return grpc.Errorf(codes.InvalidArgument,
  80. "invalid name, only 64 [a-zA-Z0-9-_.] characters allowed, and the start and end character must be [a-zA-Z0-9]")
  81. }
  82. return nil
  83. }
  84. func validateDriver(driver *api.Driver, pg plugingetter.PluginGetter, pluginType string) error {
  85. if driver == nil {
  86. // It is ok to not specify the driver. We will choose
  87. // a default driver.
  88. return nil
  89. }
  90. if driver.Name == "" {
  91. return grpc.Errorf(codes.InvalidArgument, "driver name: if driver is specified name is required")
  92. }
  93. // First check against the known drivers
  94. switch pluginType {
  95. case ipamapi.PluginEndpointType:
  96. if strings.ToLower(driver.Name) == ipamapi.DefaultIPAM {
  97. return nil
  98. }
  99. default:
  100. if networkallocator.IsBuiltInDriver(driver.Name) {
  101. return nil
  102. }
  103. }
  104. if pg == nil {
  105. return grpc.Errorf(codes.InvalidArgument, "plugin %s not supported", driver.Name)
  106. }
  107. p, err := pg.Get(driver.Name, pluginType, plugingetter.Lookup)
  108. if err != nil {
  109. return grpc.Errorf(codes.InvalidArgument, "error during lookup of plugin %s", driver.Name)
  110. }
  111. if p.IsV1() {
  112. return grpc.Errorf(codes.InvalidArgument, "legacy plugin %s of type %s is not supported in swarm mode", driver.Name, pluginType)
  113. }
  114. return nil
  115. }