@@ -23,22 +23,24 @@ import (
2323 "golang.org/x/crypto/ssh/agent"
2424)
2525
26- const schemaPattern = "^[A-Za-z][A-Za-z0-9+.-]*:"
27-
2826var (
2927 addCmd = & cobra.Command {
3028 Use : "add [options] NAME DESTINATION" ,
3129 Args : cobra .ExactArgs (2 ),
3230 Short : "Record destination for the Podman service" ,
3331 Long : `Add destination to podman configuration.
34- "destination" is of the form [user@]hostname or
35- an URI of the form ssh://[user@]hostname[:port]
32+ "destination" is one of the form:
33+ [user@]hostname (will default to ssh)
34+ ssh://[user@]hostname[:port][/path] (will obtain socket path from service, if not given.)
35+ tcp://hostname:port (not secured)
36+ unix://path (absolute path required)
3637` ,
3738 RunE : add ,
3839 ValidArgsFunction : completion .AutocompleteNone ,
3940 Example : `podman system connection add laptop server.fubar.com
4041 podman system connection add --identity ~/.ssh/dev_rsa testing ssh://[email protected] :2222 4142 podman system connection add --identity ~/.ssh/dev_rsa --port 22 production [email protected] 43+ podman system connection add debug tcp://localhost:8080
4244 ` ,
4345 }
4446
@@ -74,9 +76,9 @@ func init() {
7476}
7577
7678func add (cmd * cobra.Command , args []string ) error {
77- // Default to ssh: schema if none given
79+ // Default to ssh schema if none given
7880 dest := args [1 ]
79- if match , err := regexp .Match (schemaPattern , []byte (dest )); err != nil {
81+ if match , err := regexp .Match ("^[A-Za-z][A-Za-z0-9+.-]*://" , []byte (dest )); err != nil {
8082 return errors .Wrapf (err , "invalid destination" )
8183 } else if ! match {
8284 dest = "ssh://" + dest
@@ -87,28 +89,63 @@ func add(cmd *cobra.Command, args []string) error {
8789 return err
8890 }
8991
90- if uri .User .Username () == "" {
91- if uri .User , err = getUserInfo (uri ); err != nil {
92- return err
93- }
94- }
95-
9692 if cmd .Flags ().Changed ("socket-path" ) {
9793 uri .Path = cmd .Flag ("socket-path" ).Value .String ()
9894 }
9995
100- if cmd .Flags ().Changed ("port" ) {
101- uri .Host = net .JoinHostPort (uri .Hostname (), cmd .Flag ("port" ).Value .String ())
102- }
96+ switch uri .Scheme {
97+ case "ssh" :
98+ if uri .User .Username () == "" {
99+ if uri .User , err = getUserInfo (uri ); err != nil {
100+ return err
101+ }
102+ }
103103
104- if uri . Port () == "" {
105- uri .Host = net .JoinHostPort (uri .Hostname (), cmd .Flag ("port" ).DefValue )
106- }
104+ if cmd . Flags (). Changed ( "port" ) {
105+ uri .Host = net .JoinHostPort (uri .Hostname (), cmd .Flag ("port" ).Value . String () )
106+ }
107107
108- if uri .Path == "" || uri .Path == "/" {
109- if uri .Path , err = getUDS (cmd , uri ); err != nil {
108+ if uri .Port () == "" {
109+ uri .Host = net .JoinHostPort (uri .Hostname (), cmd .Flag ("port" ).DefValue )
110+ }
111+
112+ if uri .Path == "" || uri .Path == "/" {
113+ if uri .Path , err = getUDS (cmd , uri ); err != nil {
114+ return err
115+ }
116+ }
117+ case "unix" :
118+ if cmd .Flags ().Changed ("identity" ) {
119+ return errors .New ("--identity option not supported for unix scheme" )
120+ }
121+
122+ if cmd .Flags ().Changed ("socket-path" ) {
123+ uri .Path = cmd .Flag ("socket-path" ).Value .String ()
124+ }
125+
126+ info , err := os .Stat (uri .Path )
127+ switch {
128+ case errors .Is (err , os .ErrNotExist ):
129+ logrus .Warnf ("%q does not exists" , uri .Path )
130+ case errors .Is (err , os .ErrPermission ):
131+ logrus .Warnf ("You do not have permission to read %q" , uri .Path )
132+ case err != nil :
110133 return err
134+ case info .Mode ()& os .ModeSocket == 0 :
135+ return fmt .Errorf ("%q exists and is not a unix domain socket" , uri .Path )
136+ }
137+ case "tcp" :
138+ if cmd .Flags ().Changed ("socket-path" ) {
139+ return errors .New ("--socket-path option not supported for tcp scheme" )
140+ }
141+ if cmd .Flags ().Changed ("identity" ) {
142+ return errors .New ("--identity option not supported for tcp scheme" )
143+ }
144+ if uri .Port () == "" {
145+ return errors .New ("tcp scheme requires a port either via --port or in destination URL" )
111146 }
147+ default :
148+ logrus .Warnf ("%q unknown scheme, no validation provided" , uri .Scheme )
112149 }
113150
114151 cfg , err := config .ReadCustomConfig ()
0 commit comments