@@ -11,6 +11,7 @@ import (
11
11
"time"
12
12
13
13
dockerfileparser "github.com/docker/docker/builder/dockerfile/parser"
14
+ "github.com/docker/go-connections/nat"
14
15
"github.com/fsouza/go-dockerclient"
15
16
"github.com/golang/glog"
16
17
@@ -1100,7 +1101,7 @@ func optionallyValidateExposedPorts(config *AppConfig, repositories app.SourceRe
1100
1101
for _ , repo := range repositories {
1101
1102
if repoInfo := repo .Info (); repoInfo != nil && repoInfo .Dockerfile != nil {
1102
1103
node := repoInfo .Dockerfile .AST ()
1103
- if err := exposedPortsAreNumeric (node ); err != nil {
1104
+ if err := exposedPortsAreValid (node ); err != nil {
1104
1105
return fmt .Errorf ("the Dockerfile has an invalid EXPOSE instruction: %v" , err )
1105
1106
}
1106
1107
}
@@ -1109,11 +1110,38 @@ func optionallyValidateExposedPorts(config *AppConfig, repositories app.SourceRe
1109
1110
return nil
1110
1111
}
1111
1112
1112
- func exposedPortsAreNumeric (node * dockerfileparser.Node ) error {
1113
+ func exposedPortsAreValid (node * dockerfileparser.Node ) error {
1114
+ allErrs := make ([]error , 0 )
1115
+
1113
1116
for _ , port := range dockerfileutil .LastExposedPorts (node ) {
1114
- if _ , err := strconv .ParseInt (port , 10 , 32 ); err != nil {
1115
- return fmt .Errorf ("could not parse %q: must be numeric" , port )
1117
+ errs := make ([]string , 0 )
1118
+
1119
+ if strings .HasPrefix (port , "$" ) {
1120
+ errs = append (errs , "args are not supported for port numbers" )
1121
+ }
1122
+
1123
+ proto , port := nat .SplitProtoPort (port )
1124
+
1125
+ _ , err := strconv .ParseUint (port , 10 , 16 )
1126
+ if err != nil {
1127
+ if numError , ok := err .(* strconv.NumError ); ok {
1128
+ if numError .Err == strconv .ErrRange || numError .Err == strconv .ErrSyntax {
1129
+ errs = append (errs , "port number must be in range 0 - 65535" )
1130
+ }
1131
+ }
1132
+ }
1133
+ if len (proto ) > 0 && ! (strings .ToLower (proto ) == "tcp" || strings .ToLower (proto ) == "udp" ) {
1134
+ errs = append (errs , "protocol must be tcp or udp" )
1116
1135
}
1136
+ if len (errs ) > 0 {
1137
+ allErrs = append (allErrs , fmt .Errorf ("could not parse %q: [%v]" , port , strings .Join (errs , ", " )))
1138
+ }
1139
+
1117
1140
}
1141
+
1142
+ if len (allErrs ) > 0 {
1143
+ return kutilerrors .NewAggregate (allErrs )
1144
+ }
1145
+
1118
1146
return nil
1119
1147
}
0 commit comments