func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli { var ( isTerminal = false terminalFd uintptr ) if in != nil { if file, ok := in.(*os.File); ok { terminalFd = file.Fd() isTerminal = term.IsTerminal(terminalFd) } } if err == nil { err = out } configFile, e := auth.LoadConfig(os.Getenv("HOME")) if e != nil { fmt.Fprintf(err, "WARNING: %s\n", e) } return &DockerCli{ proto: proto, addr: addr, configFile: configFile, in: in, out: out, err: err, isTerminal: isTerminal, terminalFd: terminalFd, } }
func (cli *DockerCli) LoadConfigFile() (err error) { cli.configFile, err = auth.LoadConfig(os.Getenv("HOME")) if err != nil { fmt.Fprintf(cli.err, "WARNING: %s\n", err) } return err }
func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli { var ( isTerminal = false terminalFd uintptr ) if in != nil { if file, ok := in.(*os.File); ok { terminalFd = file.Fd() isTerminal = term.IsTerminal(terminalFd) } } if err == nil { err = out } authConfig, _ := auth.LoadConfig(os.Getenv("HOME")) return &DockerCli{ proto: proto, addr: addr, authConfig: authConfig, in: in, out: out, err: err, isTerminal: isTerminal, terminalFd: terminalFd, } }
func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { authConfig := &auth.AuthConfig{} if version > 1.1 { if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil { return err } } else { localAuthConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil && err != auth.ErrConfigFileMissing { return err } authConfig = localAuthConfig } if err := parseForm(r); err != nil { return err } registry := r.Form.Get("registry") if vars == nil { return fmt.Errorf("Missing parameter") } name := vars["name"] if version > 1.0 { w.Header().Set("Content-Type", "application/json") } sf := utils.NewStreamFormatter(version > 1.0) if err := srv.ImagePush(name, registry, w, sf, authConfig); err != nil { if sf.Used() { w.Write(sf.FormatError(err)) return nil } return err } return nil }
func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { // FIXME: Handle multiple login at once config := &auth.AuthConfig{} if err := json.NewDecoder(r.Body).Decode(config); err != nil { return err } authConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil { if err != auth.ErrConfigFileMissing { return err } authConfig = &auth.AuthConfig{} } if config.Username == authConfig.Username { config.Password = authConfig.Password } newAuthConfig := auth.NewAuthConfig(config.Username, config.Password, config.Email, srv.runtime.root) status, err := auth.Login(newAuthConfig) if err != nil { return err } if status != "" { b, err := json.Marshal(&ApiAuth{Status: status}) if err != nil { return err } writeJson(w, b) return nil } w.WriteHeader(http.StatusNoContent) return nil }
func NewRegistry(root string) *Registry { // If the auth file does not exist, keep going authConfig, _ := auth.LoadConfig(root) r := &Registry{ authConfig: authConfig, client: &http.Client{}, } r.client.Jar = cookiejar.NewCookieJar() return r }
func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) { runtimeRepo := path.Join(root, "containers") if err := os.MkdirAll(runtimeRepo, 0700); err != nil && !os.IsExist(err) { return nil, err } g, err := NewGraph(path.Join(root, "graph")) if err != nil { return nil, err } volumes, err := NewGraph(path.Join(root, "volumes")) if err != nil { return nil, err } repositories, err := NewTagStore(path.Join(root, "repositories"), g) if err != nil { return nil, fmt.Errorf("Couldn't create Tag store: %s", err) } if NetworkBridgeIface == "" { NetworkBridgeIface = DefaultNetworkBridge } netManager, err := newNetworkManager(NetworkBridgeIface) if err != nil { return nil, err } authConfig, err := auth.LoadConfig(root) if err != nil && authConfig == nil { // If the auth file does not exist, keep going return nil, err } runtime := &Runtime{ root: root, repository: runtimeRepo, containers: list.New(), networkManager: netManager, graph: g, repositories: repositories, authConfig: authConfig, idIndex: NewTruncIndex(), capabilities: &Capabilities{}, autoRestart: autoRestart, volumes: volumes, } if err := runtime.restore(); err != nil { return nil, err } return runtime, nil }
func getAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { // FIXME: Handle multiple login at once // FIXME: return specific error code if config file missing? authConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil { if err != auth.ErrConfigFileMissing { return err } authConfig = &auth.AuthConfig{} } b, err := json.Marshal(&auth.AuthConfig{Username: authConfig.Username, Email: authConfig.Email}) if err != nil { return err } writeJson(w, b) return nil }
func NewRegistry(root string) *Registry { // If the auth file does not exist, keep going authConfig, _ := auth.LoadConfig(root) httpTransport := &http.Transport{ DisableKeepAlives: true, Proxy: http.ProxyFromEnvironment, } r := &Registry{ authConfig: authConfig, client: &http.Client{ Transport: httpTransport, }, } r.client.Jar = cookiejar.NewCookieJar() return r }
func getAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { if version > 1.1 { w.WriteHeader(http.StatusNotFound) return nil } authConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil { if err != auth.ErrConfigFileMissing { return err } authConfig = &auth.AuthConfig{} } b, err := json.Marshal(&auth.AuthConfig{Username: authConfig.Username, Email: authConfig.Email}) if err != nil { return err } writeJSON(w, b) return nil }
func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error { authConfig := &auth.AuthConfig{} err := json.NewDecoder(r.Body).Decode(authConfig) if err != nil { return err } status := "" if version > 1.1 { status, err = auth.Login(authConfig, false) if err != nil { return err } } else { localAuthConfig, err := auth.LoadConfig(srv.runtime.root) if err != nil { if err != auth.ErrConfigFileMissing { return err } } if authConfig.Username == localAuthConfig.Username { authConfig.Password = localAuthConfig.Password } newAuthConfig := auth.NewAuthConfig(authConfig.Username, authConfig.Password, authConfig.Email, srv.runtime.root) status, err = auth.Login(newAuthConfig, true) if err != nil { return err } } if status != "" { b, err := json.Marshal(&APIAuth{Status: status}) if err != nil { return err } writeJSON(w, b) return nil } w.WriteHeader(http.StatusNoContent) return nil }
func NewRuntimeFromDirectory(root string) (*Runtime, error) { runtime_repo := path.Join(root, "containers") if err := os.MkdirAll(runtime_repo, 0700); err != nil && !os.IsExist(err) { return nil, err } g, err := NewGraph(path.Join(root, "graph")) if err != nil { return nil, err } repositories, err := NewTagStore(path.Join(root, "repositories"), g) if err != nil { return nil, fmt.Errorf("Couldn't create Tag store: %s", err) } netManager, err := newNetworkManager(networkBridgeIface) if err != nil { return nil, err } authConfig, err := auth.LoadConfig(root) if err != nil && authConfig == nil { // If the auth file does not exist, keep going return nil, err } runtime := &Runtime{ root: root, repository: runtime_repo, containers: list.New(), networkManager: netManager, graph: g, repositories: repositories, authConfig: authConfig, } if err := runtime.restore(); err != nil { return nil, err } return runtime, nil }
// 'docker login': login / register a user to registry service. func (cli *DockerCli) CmdLogin(args ...string) error { var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string { char := make([]byte, 1) buffer := make([]byte, 64) var i = 0 for i < len(buffer) { n, err := stdin.Read(char) if n > 0 { if char[0] == '\r' || char[0] == '\n' { stdout.Write([]byte{'\r', '\n'}) break } else if char[0] == 127 || char[0] == '\b' { if i > 0 { if echo { stdout.Write([]byte{'\b', ' ', '\b'}) } i-- } } else if !unicode.IsSpace(rune(char[0])) && !unicode.IsControl(rune(char[0])) { if echo { stdout.Write(char) } buffer[i] = char[0] i++ } } if err != nil { if err != io.EOF { fmt.Fprintf(stdout, "Read error: %v\r\n", err) } break } } return string(buffer[:i]) } var readAndEchoString = func(stdin io.Reader, stdout io.Writer) string { return readStringOnRawTerminal(stdin, stdout, true) } var readString = func(stdin io.Reader, stdout io.Writer) string { return readStringOnRawTerminal(stdin, stdout, false) } cmd := Subcmd("login", "[OPTIONS]", "Register or Login to the docker registry server") flUsername := cmd.String("u", "", "username") flPassword := cmd.String("p", "", "password") flEmail := cmd.String("e", "", "email") err := cmd.Parse(args) if err != nil { return nil } var oldState *term.State if *flUsername == "" || *flPassword == "" || *flEmail == "" { oldState, err = term.SetRawTerminal(cli.terminalFd) if err != nil { return err } defer term.RestoreTerminal(cli.terminalFd, oldState) } var ( username string password string email string ) if *flUsername == "" { fmt.Fprintf(cli.out, "Username (%s): ", cli.authConfig.Username) username = readAndEchoString(cli.in, cli.out) if username == "" { username = cli.authConfig.Username } } else { username = *flUsername } if username != cli.authConfig.Username { if *flPassword == "" { fmt.Fprintf(cli.out, "Password: "******"" { return fmt.Errorf("Error : Password Required") } } else { password = *flPassword } if *flEmail == "" { fmt.Fprintf(cli.out, "Email (%s): ", cli.authConfig.Email) email = readAndEchoString(cli.in, cli.out) if email == "" { email = cli.authConfig.Email } } else { email = *flEmail } } else { password = cli.authConfig.Password email = cli.authConfig.Email } if oldState != nil { term.RestoreTerminal(cli.terminalFd, oldState) } cli.authConfig.Username = username cli.authConfig.Password = password cli.authConfig.Email = email body, statusCode, err := cli.call("POST", "/auth", cli.authConfig) if statusCode == 401 { cli.authConfig.Username = "" cli.authConfig.Password = "" cli.authConfig.Email = "" auth.SaveConfig(cli.authConfig) return err } if err != nil { return err } var out2 APIAuth err = json.Unmarshal(body, &out2) if err != nil { auth.LoadConfig(os.Getenv("HOME")) return err } auth.SaveConfig(cli.authConfig) if out2.Status != "" { fmt.Fprintf(cli.out, "%s\n", out2.Status) } return nil }
// 'docker login': login / register a user to registry service. func (cli *DockerCli) CmdLogin(args ...string) error { var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string { char := make([]byte, 1) buffer := make([]byte, 64) var i = 0 for i < len(buffer) { n, err := stdin.Read(char) if n > 0 { if char[0] == '\r' || char[0] == '\n' { stdout.Write([]byte{'\r', '\n'}) break } else if char[0] == 127 || char[0] == '\b' { if i > 0 { if echo { stdout.Write([]byte{'\b', ' ', '\b'}) } i-- } } else if !unicode.IsSpace(rune(char[0])) && !unicode.IsControl(rune(char[0])) { if echo { stdout.Write(char) } buffer[i] = char[0] i++ } } if err != nil { if err != io.EOF { fmt.Fprintf(stdout, "Read error: %v\r\n", err) } break } } return string(buffer[:i]) } var readAndEchoString = func(stdin io.Reader, stdout io.Writer) string { return readStringOnRawTerminal(stdin, stdout, true) } var readString = func(stdin io.Reader, stdout io.Writer) string { return readStringOnRawTerminal(stdin, stdout, false) } oldState, err := term.SetRawTerminal() if err != nil { return err } defer term.RestoreTerminal(oldState) cmd := Subcmd("login", "", "Register or Login to the docker registry server") if err := cmd.Parse(args); err != nil { return nil } var username string var password string var email string fmt.Print("Username (", cli.authConfig.Username, "): ") username = readAndEchoString(os.Stdin, os.Stdout) if username == "" { username = cli.authConfig.Username } if username != cli.authConfig.Username { fmt.Print("Password: "******"" { return fmt.Errorf("Error : Password Required") } fmt.Print("Email (", cli.authConfig.Email, "): ") email = readAndEchoString(os.Stdin, os.Stdout) if email == "" { email = cli.authConfig.Email } } else { email = cli.authConfig.Email } term.RestoreTerminal(oldState) cli.authConfig.Username = username cli.authConfig.Password = password cli.authConfig.Email = email body, _, err := cli.call("POST", "/auth", cli.authConfig) if err != nil { return err } var out2 APIAuth err = json.Unmarshal(body, &out2) if err != nil { auth.LoadConfig(os.Getenv("HOME")) return err } auth.SaveConfig(cli.authConfig) if out2.Status != "" { fmt.Print(out2.Status) } return nil }
// 'docker login': login / register a user to registry service. func (cli *DockerCli) CmdLogin(args ...string) error { cmd := Subcmd("login", "[OPTIONS] [SERVER]", "Register or Login to a docker registry server, if no server is specified \""+auth.IndexServerAddress()+"\" is the default.") var username, password, email string cmd.StringVar(&username, "u", "", "username") cmd.StringVar(&password, "p", "", "password") cmd.StringVar(&email, "e", "", "email") err := cmd.Parse(args) if err != nil { return nil } serverAddress := auth.IndexServerAddress() if len(cmd.Args()) > 0 { serverAddress, err = registry.ExpandAndVerifyRegistryUrl(cmd.Arg(0)) if err != nil { return err } fmt.Fprintf(cli.out, "Login against server at %s\n", serverAddress) } promptDefault := func(prompt string, configDefault string) { if configDefault == "" { fmt.Fprintf(cli.out, "%s: ", prompt) } else { fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault) } } readInput := func(in io.Reader, out io.Writer) string { reader := bufio.NewReader(in) line, _, err := reader.ReadLine() if err != nil { fmt.Fprintln(out, err.Error()) os.Exit(1) } return string(line) } cli.LoadConfigFile() authconfig, ok := cli.configFile.Configs[auth.IndexServerAddress()] if !ok { authconfig = auth.AuthConfig{} } if username == "" { promptDefault("Username", authconfig.Username) username = readInput(cli.in, cli.out) if username == "" { username = authconfig.Username } } if username != authconfig.Username { if password == "" { oldState, _ := term.SaveState(cli.terminalFd) fmt.Fprintf(cli.out, "Password: "******"\n") term.RestoreTerminal(cli.terminalFd, oldState) if password == "" { return fmt.Errorf("Error : Password Required") } } if email == "" { promptDefault("Email", authconfig.Email) email = readInput(cli.in, cli.out) if email == "" { email = authconfig.Email } } } else { password = authconfig.Password email = authconfig.Email } authconfig.Username = username authconfig.Password = password authconfig.Email = email authconfig.ServerAddress = serverAddress cli.configFile.Configs[serverAddress] = authconfig body, statusCode, err := cli.call("POST", "/auth", cli.configFile.Configs[serverAddress]) if statusCode == 401 { delete(cli.configFile.Configs, serverAddress) auth.SaveConfig(cli.configFile) return err } if err != nil { return err } var out2 APIAuth err = json.Unmarshal(body, &out2) if err != nil { cli.configFile, _ = auth.LoadConfig(os.Getenv("HOME")) return err } auth.SaveConfig(cli.configFile) if out2.Status != "" { fmt.Fprintf(cli.out, "%s\n", out2.Status) } return nil }
func NewDockerCli(proto, addr string) *DockerCli { authConfig, _ := auth.LoadConfig(os.Getenv("HOME")) return &DockerCli{proto, addr, authConfig} }
func NewDockerCli(addr string, port int) *DockerCli { authConfig, _ := auth.LoadConfig(os.Getenv("HOME")) return &DockerCli{addr, port, authConfig} }