func (z *zkCluster) GetQueryNodes() ([]clustering.QueryNode, errors.Error) { impl, ok := getConfigStoreImplementation(z) if !ok { return nil, errors.NewAdminConnectionError(nil, z.ConfigurationStoreId()) } qryNodes := []clustering.QueryNode{} nodes, _, err := impl.conn.Children("/" + z.Name()) if err != nil { return nil, errors.NewAdminGetClusterError(err, z.Name()) } for _, name := range nodes { nodePath := "/" + z.Name() + "/" + name data, _, err := impl.conn.Get(nodePath) if err != nil { return nil, errors.NewAdminGetNodeError(err, nodePath) } queryNode := &zkQueryNodeConfig{} err = json.Unmarshal(data, queryNode) if err != nil { return nil, errors.NewAdminDecodingError(err) } qryNodes = append(qryNodes, queryNode) } return qryNodes, nil }
func getNodeFromRequest(req *http.Request) (clustering.QueryNode, errors.Error) { var node clustering.QueryNode decoder := json.NewDecoder(req.Body) err := decoder.Decode(&node) if err != nil { return nil, errors.NewAdminDecodingError(err) } return node, nil }
func getClusterFromRequest(req *http.Request) (clustering.Cluster, errors.Error) { var cluster clustering.Cluster decoder := json.NewDecoder(req.Body) err := decoder.Decode(&cluster) if err != nil { return nil, errors.NewAdminDecodingError(err) } return cluster, nil }
func (z *zkConfigStore) ClusterByName(name string) (clustering.Cluster, errors.Error) { data, _, err := z.conn.Get("/" + name) if err != nil { return nil, errors.NewAdminGetClusterError(err, name) } var clusterConfig zkCluster err = json.Unmarshal(data, &clusterConfig) if err != nil { return nil, errors.NewAdminDecodingError(err) } clusterConfig.configStore = z return &clusterConfig, nil }
func (z *zkCluster) QueryNodeByName(name string) (clustering.QueryNode, errors.Error) { impl, ok := getConfigStoreImplementation(z) if !ok { return nil, errors.NewAdminConnectionError(nil, z.ConfigurationStoreId()) } nodePath := "/" + z.ClusterName + "/" + name data, _, err := impl.conn.Get(nodePath) if err != nil { return nil, errors.NewAdminGetNodeError(err, nodePath) } var queryNode zkQueryNodeConfig err = json.Unmarshal(data, &queryNode) if err != nil { return nil, errors.NewAdminDecodingError(err) } return &queryNode, nil }
func (this *HttpEndpoint) wrapAPI(w http.ResponseWriter, req *http.Request, f apiFunc) { obj, err := f(this, w, req) if err != nil { writeError(w, err) return } if obj == nil { w.WriteHeader(http.StatusNotFound) return } buf, json_err := json.Marshal(obj) if json_err != nil { writeError(w, errors.NewAdminDecodingError(json_err)) return } w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write(buf) }
func (this *HttpEndpoint) hasAdminAuth(req *http.Request) errors.Error { // retrieve the credentials from the request; the credentials must be specified // using basic authorization format. An error is returned if there is a step that // prevents retrieval of the credentials. authHdr := req.Header["Authorization"] if len(authHdr) == 0 { return errors.NewAdminAuthError(nil, "basic authorization required") } auth := authHdr[0] basicPrefix := "Basic " if !strings.HasPrefix(auth, basicPrefix) { return errors.NewAdminAuthError(nil, "basic authorization required") } decoded, err := base64.StdEncoding.DecodeString(auth[len(basicPrefix):]) if err != nil { return errors.NewAdminDecodingError(err) } colonIndex := bytes.IndexByte(decoded, ':') if colonIndex == -1 { return errors.NewAdminAuthError(nil, "incorrect authorization header") } user := string(decoded[:colonIndex]) password := string(decoded[colonIndex+1:]) creds := map[string]string{user: password} // Attempt authorization with the cluster configstore := this.server.ConfigurationStore() sslPrivs := []clustering.Privilege{clustering.PRIV_SYS_ADMIN} authErr := configstore.Authorize(creds, sslPrivs) if authErr != nil { return authErr } return nil }
func (z *zkConfigStore) GetClusters() ([]clustering.Cluster, errors.Error) { clusters := []clustering.Cluster{} nodes, _, err := z.conn.Children("/") if err != nil { return nil, errors.NewAdminGetClusterError(err, "/") } for _, name := range nodes { if name == _RESERVED_NAME { continue } data, _, err := z.conn.Get("/" + name) if err != nil { return nil, errors.NewAdminGetClusterError(err, name) } cluster := &zkCluster{} err = json.Unmarshal(data, cluster) if err != nil { return nil, errors.NewAdminDecodingError(err) } clusters = append(clusters, cluster) } return clusters, nil }
func doSettings(endpoint *HttpEndpoint, w http.ResponseWriter, req *http.Request) (interface{}, errors.Error) { // Admin auth required err := endpoint.hasAdminAuth(req) if err != nil { return nil, err } settings := map[string]interface{}{} srvr := endpoint.server switch req.Method { case "GET": return fillSettings(settings, srvr), nil case "POST": decoder := json.NewDecoder(req.Body) err := decoder.Decode(&settings) if err != nil { return nil, errors.NewAdminDecodingError(err) } for setting, value := range settings { if check_it, ok := _CHECKERS[setting]; !ok { return nil, errors.NewAdminUnknownSettingError(setting) } else { if !check_it(value) { return nil, errors.NewAdminSettingTypeError(setting, value) } } } for setting, value := range settings { set_it := _SETTERS[setting] set_it(srvr, value) } return fillSettings(settings, srvr), nil default: return nil, nil } }