perkeep/pkg/httputil/auth.go

102 lines
2.8 KiB
Go
Raw Normal View History

buildbot/master: add Basic Auth support. Moved BasicAuth parsing and localhost detection code from pkg/auth -> pkg/httputil for use by buildbot master. Added user config file for remote access. The file's name is "masterbot-config.json" and is located in osutil.CamliConfigDir(), which on Unix will resolve to $XDG_CONFIG_HOME/camlistore/, if XDG_CONFIG_HOME set, or ~/.config/camlistore/. On Windows it will be under %APPDATA%\Camlistore\. The expected format is a json object with usernames as the keys and sha1 sums of the password as the values, i.e.: { "user1": "1234567890abcdef12341234567890abcdef1234", "user2": "1234abcdef12345678901234abcdef1234567890" } This file is polled at a 1 minute interval and reparsed if the file's modification time is more recent then the previous parse attempt. It is ok for the file to go missing, it will zero out the remote user list. A malformed file will result in the master exiting. New commandline flags, -tlsCertFile & -tlsKeyFile, added. Specifying both will enable TLS on the listener specified by -host. The go source contains generate_cert.go in crypto/tls that can be used to generate self-signed cert.pem and key.pem for testing. Added -skiptlscheck commandline option to builder. This allows the builder to report to https:// addresses with self-signed certs as we don't currently have a way to specify the cert chains to be used for TLS verification. This is a stop-gap solution. When launching a master that listens for secure connections, we currently need tell the builders to skip certificate validation. Add '-builderopts="-skiptlscheck"' to the master's commandline to skip cerfication verification. Change-Id: I0750b5c9fa8f4def67fc05a841087b50abded2f7
2013-10-31 05:00:17 +00:00
/*
Copyright 2013 The Camlistore Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package httputil
import (
"encoding/base64"
"fmt"
"log"
buildbot/master: add Basic Auth support. Moved BasicAuth parsing and localhost detection code from pkg/auth -> pkg/httputil for use by buildbot master. Added user config file for remote access. The file's name is "masterbot-config.json" and is located in osutil.CamliConfigDir(), which on Unix will resolve to $XDG_CONFIG_HOME/camlistore/, if XDG_CONFIG_HOME set, or ~/.config/camlistore/. On Windows it will be under %APPDATA%\Camlistore\. The expected format is a json object with usernames as the keys and sha1 sums of the password as the values, i.e.: { "user1": "1234567890abcdef12341234567890abcdef1234", "user2": "1234abcdef12345678901234abcdef1234567890" } This file is polled at a 1 minute interval and reparsed if the file's modification time is more recent then the previous parse attempt. It is ok for the file to go missing, it will zero out the remote user list. A malformed file will result in the master exiting. New commandline flags, -tlsCertFile & -tlsKeyFile, added. Specifying both will enable TLS on the listener specified by -host. The go source contains generate_cert.go in crypto/tls that can be used to generate self-signed cert.pem and key.pem for testing. Added -skiptlscheck commandline option to builder. This allows the builder to report to https:// addresses with self-signed certs as we don't currently have a way to specify the cert chains to be used for TLS verification. This is a stop-gap solution. When launching a master that listens for secure connections, we currently need tell the builders to skip certificate validation. Add '-builderopts="-skiptlscheck"' to the master's commandline to skip cerfication verification. Change-Id: I0750b5c9fa8f4def67fc05a841087b50abded2f7
2013-10-31 05:00:17 +00:00
"net/http"
"os"
"regexp"
"runtime"
"strings"
"camlistore.org/pkg/netutil"
)
var kBasicAuthPattern = regexp.MustCompile(`^Basic ([a-zA-Z0-9\+/=]+)`)
// IsLocalhost reports whether the requesting connection is from this machine
// and has the same owner as this process.
func IsLocalhost(req *http.Request) bool {
uid := os.Getuid()
from, err := netutil.HostPortToIP(req.RemoteAddr, nil)
if err != nil {
return false
}
to, err := netutil.HostPortToIP(req.Host, from)
if err != nil {
return false
}
// If our OS doesn't support uid.
// TODO(bradfitz): netutil on OS X uses "lsof" to figure out
// ownership of tcp connections, but when fuse is mounted and a
// request is outstanding (for instance, a fuse request that's
// making a request to camlistored and landing in this code
// path), lsof then blocks forever waiting on a lock held by the
// VFS, leading to a deadlock. Instead, on darwin, just trust
// any localhost connection here, which is kinda lame, but
// whatever. Macs aren't very multi-user anyway.
if uid == -1 || runtime.GOOS == "darwin" {
return from.IP.IsLoopback() && to.IP.IsLoopback()
}
if uid == 0 {
log.Printf("camlistored running as root. Don't do that.")
return false
}
buildbot/master: add Basic Auth support. Moved BasicAuth parsing and localhost detection code from pkg/auth -> pkg/httputil for use by buildbot master. Added user config file for remote access. The file's name is "masterbot-config.json" and is located in osutil.CamliConfigDir(), which on Unix will resolve to $XDG_CONFIG_HOME/camlistore/, if XDG_CONFIG_HOME set, or ~/.config/camlistore/. On Windows it will be under %APPDATA%\Camlistore\. The expected format is a json object with usernames as the keys and sha1 sums of the password as the values, i.e.: { "user1": "1234567890abcdef12341234567890abcdef1234", "user2": "1234abcdef12345678901234abcdef1234567890" } This file is polled at a 1 minute interval and reparsed if the file's modification time is more recent then the previous parse attempt. It is ok for the file to go missing, it will zero out the remote user list. A malformed file will result in the master exiting. New commandline flags, -tlsCertFile & -tlsKeyFile, added. Specifying both will enable TLS on the listener specified by -host. The go source contains generate_cert.go in crypto/tls that can be used to generate self-signed cert.pem and key.pem for testing. Added -skiptlscheck commandline option to builder. This allows the builder to report to https:// addresses with self-signed certs as we don't currently have a way to specify the cert chains to be used for TLS verification. This is a stop-gap solution. When launching a master that listens for secure connections, we currently need tell the builders to skip certificate validation. Add '-builderopts="-skiptlscheck"' to the master's commandline to skip cerfication verification. Change-Id: I0750b5c9fa8f4def67fc05a841087b50abded2f7
2013-10-31 05:00:17 +00:00
if uid > 0 {
connUid, err := netutil.AddrPairUserid(from, to)
if err == nil {
if uid == connUid {
return true
}
log.Printf("auth: local connection uid %d doesn't match server uid %d", connUid, uid)
buildbot/master: add Basic Auth support. Moved BasicAuth parsing and localhost detection code from pkg/auth -> pkg/httputil for use by buildbot master. Added user config file for remote access. The file's name is "masterbot-config.json" and is located in osutil.CamliConfigDir(), which on Unix will resolve to $XDG_CONFIG_HOME/camlistore/, if XDG_CONFIG_HOME set, or ~/.config/camlistore/. On Windows it will be under %APPDATA%\Camlistore\. The expected format is a json object with usernames as the keys and sha1 sums of the password as the values, i.e.: { "user1": "1234567890abcdef12341234567890abcdef1234", "user2": "1234abcdef12345678901234abcdef1234567890" } This file is polled at a 1 minute interval and reparsed if the file's modification time is more recent then the previous parse attempt. It is ok for the file to go missing, it will zero out the remote user list. A malformed file will result in the master exiting. New commandline flags, -tlsCertFile & -tlsKeyFile, added. Specifying both will enable TLS on the listener specified by -host. The go source contains generate_cert.go in crypto/tls that can be used to generate self-signed cert.pem and key.pem for testing. Added -skiptlscheck commandline option to builder. This allows the builder to report to https:// addresses with self-signed certs as we don't currently have a way to specify the cert chains to be used for TLS verification. This is a stop-gap solution. When launching a master that listens for secure connections, we currently need tell the builders to skip certificate validation. Add '-builderopts="-skiptlscheck"' to the master's commandline to skip cerfication verification. Change-Id: I0750b5c9fa8f4def67fc05a841087b50abded2f7
2013-10-31 05:00:17 +00:00
}
}
return false
}
// BasicAuth parses the Authorization header on req
// If absent or invalid, an error is returned.
func BasicAuth(req *http.Request) (username, password string, err error) {
auth := req.Header.Get("Authorization")
if auth == "" {
err = fmt.Errorf("Missing \"Authorization\" in header")
return
}
matches := kBasicAuthPattern.FindStringSubmatch(auth)
if len(matches) != 2 {
err = fmt.Errorf("Bogus Authorization header")
return
}
encoded := matches[1]
enc := base64.StdEncoding
decBuf := make([]byte, enc.DecodedLen(len(encoded)))
n, err := enc.Decode(decBuf, []byte(encoded))
if err != nil {
return
}
pieces := strings.SplitN(string(decBuf[0:n]), ":", 2)
if len(pieces) != 2 {
err = fmt.Errorf("didn't get two pieces")
return
}
return pieces[0], pieces[1], nil
}