Example #1
0
func TestAuth(t *testing.T) {
	a, b, err := netPipe()
	if err != nil {
		t.Fatalf("netPipe: %v", err)
	}

	defer a.Close()
	defer b.Close()

	agent, _, cleanup := startAgent(t)
	defer cleanup()

	if err := agent.Add(testPrivateKeys["rsa"], nil, "comment"); err != nil {
		t.Errorf("Add: %v", err)
	}

	serverConf := ssh.ServerConfig{}
	serverConf.AddHostKey(testSigners["rsa"])
	serverConf.PublicKeyCallback = func(c ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
		if bytes.Equal(key.Marshal(), testPublicKeys["rsa"].Marshal()) {
			return nil, nil
		}

		return nil, errors.New("pubkey rejected")
	}

	go func() {
		conn, _, _, err := ssh.NewServerConn(a, &serverConf)
		if err != nil {
			t.Fatalf("Server: %v", err)
		}
		conn.Close()
	}()

	conf := ssh.ClientConfig{}
	conf.Auth = append(conf.Auth, ssh.PublicKeysCallback(agent.Signers))
	conn, _, _, err := ssh.NewClientConn(b, "", &conf)
	if err != nil {
		t.Fatalf("NewClientConn: %v", err)
	}
	conn.Close()
}
Example #2
0
func submitssh(srcs, dsts []string, submitdata, runbuf io.Reader) {
	if !*cpy && *n < 1 {
		return
	}

	// use ssh agent
	agentconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err != nil {
		log.Fatal(err)
	}
	defer agentconn.Close()
	ag := agent.NewClient(agentconn)
	config := &ssh.ClientConfig{
		User: *user,
		Auth: []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)},
	}

	// connect to server (with optional hops)
	var client *ssh.Client
	if *via != "" {
		client, err = ssh.Dial("tcp", *via, config)
		if err != nil {
			log.Fatal(err)
		}
	}

	if client != nil && *dst != "" {
		client, err = Hop(client, *dst, config)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		client, err = ssh.Dial("tcp", *dst, config)
		if err != nil {
			log.Fatal(err)
		}
	}

	// copy files
	err = copyFile(client, submitdata, condorname)
	if err != nil {
		log.Fatal(err)
	}

	err = copyFile(client, runbuf, runfilename)
	if err != nil {
		log.Fatal(err)
	}

	if *cpy {
		for i, name := range srcs {

			f, err := os.Open(name)
			if err != nil {
				log.Fatal(err)
			}
			err = copyFile(client, f, dsts[i])
			if err != nil {
				log.Fatal(err)
			}
			f.Close()
		}
	}

	if *n > 0 {
		out, err := combined(client, "condor_submit "+condorname)
		if err != nil {
			fmt.Printf("%s\n", out)
			log.Fatal(err)
		}
	}
}