Changeset 771 in code


Ignore:
Timestamp:
Feb 4, 2022, 3:47:34 PM (3 years ago)
Author:
contact
Message:

service: switch to -network flag for certfp and sasl commands

Instead of always requiring users to explicitly specify the network
name, guess it from the downstream connection.

Network commands are left as-is because it's not yet clear how to
handle them.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/soju.1.scd

    r730 r771  
    326326                        Currently same as *message*. This is the default behaviour.
    327327
    328 *certfp generate* [options...] <network name>
     328*certfp generate* [options...]
    329329        Generate self-signed certificate and use it for authentication (via SASL
    330330        EXTERNAL).
     
    333333
    334334        Options are:
     335
     336        *-network* <name>
     337                Select a network. By default, the current network is selected, if any.
    335338
    336339        *-key-type* <type>
     
    341344                Size of RSA key to generate. Ignored for other key types.
    342345
    343 *certfp fingerprint* <network name>
     346*certfp fingerprint* [options...]
    344347        Show SHA-1 and SHA-256 fingerprints for the certificate
    345348        currently used with the network.
    346349
    347 *sasl status* <network name>
     350        Options are:
     351
     352        *-network* <name>
     353                Select a network. By default, the current network is selected, if any.
     354
     355*sasl status* [options...]
    348356        Show current SASL status.
    349357
    350 *sasl set-plain* <network name> <username> <password>
     358        Options are:
     359
     360        *-network* <name>
     361                Select a network. By default, the current network is selected, if any.
     362
     363*sasl set-plain* [options...] <username> <password>
    351364        Set SASL PLAIN credentials.
    352365
    353 *sasl reset* <network name>
     366        Options are:
     367
     368        *-network* <name>
     369                Select a network. By default, the current network is selected, if any.
     370
     371*sasl reset* [options...]
    354372        Disable SASL authentication and remove stored credentials.
     373
     374        Options are:
     375
     376        *-network* <name>
     377                Select a network. By default, the current network is selected, if any.
    355378
    356379*user create* -username <username> -password <password> [options...]
  • trunk/service.go

    r770 r771  
    77        "crypto/sha512"
    88        "encoding/hex"
    9         "errors"
    109        "flag"
    1110        "fmt"
     
    229228                        children: serviceCommandSet{
    230229                                "generate": {
    231                                         usage:  "[-key-type rsa|ecdsa|ed25519] [-bits N] <network name>",
     230                                        usage:  "[-key-type rsa|ecdsa|ed25519] [-bits N] [-network name]",
    232231                                        desc:   "generate a new self-signed certificate, defaults to using RSA-3072 key",
    233232                                        handle: handleServiceCertFPGenerate,
    234233                                },
    235234                                "fingerprint": {
    236                                         usage:  "<network name>",
    237                                         desc:   "show fingerprints of certificate associated with the network",
     235                                        usage:  "[-network name]",
     236                                        desc:   "show fingerprints of certificate",
    238237                                        handle: handleServiceCertFPFingerprints,
    239238                                },
     
    243242                        children: serviceCommandSet{
    244243                                "status": {
    245                                         usage:  "<network name>",
     244                                        usage:  "[-network name]",
    246245                                        desc:   "show SASL status",
    247246                                        handle: handleServiceSASLStatus,
    248247                                },
    249248                                "set-plain": {
    250                                         usage:  "<network name> <username> <password>",
     249                                        usage:  "[-network name] <username> <password>",
    251250                                        desc:   "set SASL PLAIN credentials",
    252251                                        handle: handleServiceSASLSetPlain,
    253252                                },
    254253                                "reset": {
    255                                         usage:  "<network name>",
     254                                        usage:  "[-network name]",
    256255                                        desc:   "disable SASL authentication and remove stored credentials",
    257256                                        handle: handleServiceSASLReset,
     
    632631}
    633632
     633func getNetworkFromFlag(dc *downstreamConn, name string) (*network, error) {
     634        if name == "" {
     635                if dc.network == nil {
     636                        return nil, fmt.Errorf("no network selected, -network is required")
     637                }
     638                return dc.network, nil
     639        } else {
     640                net := dc.user.getNetwork(name)
     641                if net == nil {
     642                        return nil, fmt.Errorf("unknown network %q", name)
     643                }
     644                return net, nil
     645        }
     646}
     647
    634648func handleServiceCertFPGenerate(ctx context.Context, dc *downstreamConn, params []string) error {
    635649        fs := newFlagSet()
     650        netName := fs.String("network", "", "select a network")
    636651        keyType := fs.String("key-type", "rsa", "key type to generate (rsa, ecdsa, ed25519)")
    637652        bits := fs.Int("bits", 3072, "size of key to generate, meaningful only for RSA")
     
    641656        }
    642657
    643         if len(fs.Args()) != 1 {
    644                 return errors.New("exactly one argument is required")
    645         }
    646 
    647         net := dc.user.getNetwork(fs.Arg(0))
    648         if net == nil {
    649                 return fmt.Errorf("unknown network %q", fs.Arg(0))
    650         }
    651 
    652658        if *bits <= 0 || *bits > maxRSABits {
    653659                return fmt.Errorf("invalid value for -bits")
     660        }
     661
     662        net, err := getNetworkFromFlag(dc, *netName)
     663        if err != nil {
     664                return err
    654665        }
    655666
     
    673684
    674685func handleServiceCertFPFingerprints(ctx context.Context, dc *downstreamConn, params []string) error {
    675         if len(params) != 1 {
    676                 return fmt.Errorf("expected exactly one argument")
    677         }
    678 
    679         net := dc.user.getNetwork(params[0])
    680         if net == nil {
    681                 return fmt.Errorf("unknown network %q", params[0])
     686        fs := newFlagSet()
     687        netName := fs.String("network", "", "select a network")
     688
     689        if err := fs.Parse(params); err != nil {
     690                return err
     691        }
     692
     693        net, err := getNetworkFromFlag(dc, *netName)
     694        if err != nil {
     695                return err
    682696        }
    683697
     
    691705
    692706func handleServiceSASLStatus(ctx context.Context, dc *downstreamConn, params []string) error {
    693         if len(params) != 1 {
    694                 return fmt.Errorf("expected exactly one argument")
    695         }
    696 
    697         net := dc.user.getNetwork(params[0])
    698         if net == nil {
    699                 return fmt.Errorf("unknown network %q", params[0])
     707        fs := newFlagSet()
     708        netName := fs.String("network", "", "select a network")
     709
     710        if err := fs.Parse(params); err != nil {
     711                return err
     712        }
     713
     714        net, err := getNetworkFromFlag(dc, *netName)
     715        if err != nil {
     716                return err
    700717        }
    701718
     
    723740
    724741func handleServiceSASLSetPlain(ctx context.Context, dc *downstreamConn, params []string) error {
    725         if len(params) != 3 {
    726                 return fmt.Errorf("expected exactly 3 arguments")
    727         }
    728 
    729         net := dc.user.getNetwork(params[0])
    730         if net == nil {
    731                 return fmt.Errorf("unknown network %q", params[0])
    732         }
    733 
    734         net.SASL.Plain.Username = params[1]
    735         net.SASL.Plain.Password = params[2]
     742        fs := newFlagSet()
     743        netName := fs.String("network", "", "select a network")
     744
     745        if err := fs.Parse(params); err != nil {
     746                return err
     747        }
     748
     749        if len(fs.Args()) != 2 {
     750                return fmt.Errorf("expected exactly 2 arguments")
     751        }
     752
     753        net, err := getNetworkFromFlag(dc, *netName)
     754        if err != nil {
     755                return err
     756        }
     757
     758        net.SASL.Plain.Username = fs.Arg(0)
     759        net.SASL.Plain.Password = fs.Arg(1)
    736760        net.SASL.Mechanism = "PLAIN"
    737761
     
    745769
    746770func handleServiceSASLReset(ctx context.Context, dc *downstreamConn, params []string) error {
    747         if len(params) != 1 {
    748                 return fmt.Errorf("expected exactly one argument")
    749         }
    750 
    751         net := dc.user.getNetwork(params[0])
    752         if net == nil {
    753                 return fmt.Errorf("unknown network %q", params[0])
     771        fs := newFlagSet()
     772        netName := fs.String("network", "", "select a network")
     773
     774        if err := fs.Parse(params); err != nil {
     775                return err
     776        }
     777
     778        net, err := getNetworkFromFlag(dc, *netName)
     779        if err != nil {
     780                return err
    754781        }
    755782
Note: See TracChangeset for help on using the changeset viewer.