Esempio n. 1
0
// deleteEntries deletes DNS entries, given a stack name, and list of Entries to delete.
func (cli *CloudFlareDNS) deleteEntries(name string, e *Entries) error {
	ctx := context.TODO()

	client := cli.client()

	// First we need to find our zone
	zone, err := cli.findZone(ctx, e.Zone)
	if err != nil {
		return errgo.Mask(err, errgo.Any)
	}

	records, err := client.Records.List(ctx, zone.ID)
	if err != nil {
		return errgo.Mask(err, errgo.Any)
	}

	for _, record := range records {
		entriesRecord := record.Name == e.Catchall ||
			record.Name == e.CatchallPrivate ||
			record.Name == e.Public ||
			record.Name == e.Private ||
			record.Name == e.Fleet
		if entriesRecord {
			if err := client.Records.Delete(ctx, zone.ID, record.ID); err != nil {
				return errgo.Mask(err, errgo.Any)
			}
		}
	}
	return nil
}
Esempio n. 2
0
// fetches or catches result of fn as JSON, and renders JSON to response
func (m *memcacheCache) render(w http.ResponseWriter, status int, key string, fn func() (interface{}, error)) error {

	var write = func(value []byte) error {
		return writeBody(w, value, status, "application/json")
	}

	key = makeCacheKey(key)

	it, err := m.mc.Get(key)
	if err == nil {
		return write(it.Value)
	} else if err != memcache.ErrCacheMiss {
		return errgo.Mask(err)
	}
	obj, err := fn()
	if err != nil {
		return err
	}
	value, err := m.set(key, obj)
	if err != nil {
		return errgo.Mask(err)
	}
	return write(value)

}
Esempio n. 3
0
func parseParametersTemplate(templatePath string, p parameters) (string, error) {
	f, err := os.Create(generatedParametersPath)
	if err != nil {
		return generatedParametersPath, errgo.Mask(err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()

	absoluteParametersTemplatePath, err := filepath.Abs(templatePath)
	if err != nil {
		return "", errgo.Mask(err)
	}

	templateData, err := ioutil.ReadFile(absoluteParametersTemplatePath)
	if err != nil {
		return "", errgo.Mask(err)
	}

	var tmpl *template.Template
	if tmpl, err = template.New("cfg").Parse(string(templateData)); err != nil {
		return generatedParametersPath, errgo.Mask(err)
	}

	if err = tmpl.Execute(f, p); err != nil {
		return generatedParametersPath, errgo.Mask(err)
	}

	return generatedParametersPath, nil
}
Esempio n. 4
0
func InitEntityStoreMap(dbrSess *dbr.Session) error {
	if atomic.LoadUint32(&initMapDone.done) == 1 {
		return ErrStoreMapInitialized
	}

	initMapDone.m.Lock()
	defer initMapDone.m.Unlock()
	if initMapDone.done == 0 {
		defer atomic.StoreUint32(&initMapDone.done, 1)

		s, err := TableCollection.Structure(TableIndexEntityStore)
		if err != nil {
			return errgo.Mask(err)
		}
		var ess TableEntityStoreSlice
		_, err = dbrSess.
			Select(s.Columns.FieldNames()...).
			From(s.Name).
			LoadStructs(&ess)
		if err != nil {
			return errgo.Mask(err)
		}

		for _, es := range ess {
			EntityStoreMap.Set(es.EntityTypeID, es.StoreID, es)
		}

		ess = ess[:len(ess)-1] // delete Struct Slice https://code.google.com/p/go-wiki/wiki/SliceTricks
		return nil
	}
	return ErrStoreMapInitialized
}
Esempio n. 5
0
// SQLQueryToColumns generates from a SQL query an array containing all the column properties.
// dbSelect argument can be nil but then you must provide query strings which will be joined to the final query.
func SQLQueryToColumns(db *sql.DB, dbSelect *dbr.SelectBuilder, query ...string) (Columns, error) {

	tableName := "tmp_" + randSeq(20)
	dropTable := func() {
		_, err := db.Exec("DROP TABLE IF EXISTS `" + tableName + "`")
		if err != nil {
			panic(err)
		}
	}
	dropTable()
	defer dropTable()

	qry := strings.Join(query, " ")
	var args []interface{}
	if qry == "" && dbSelect != nil {
		var err error
		qry, args, err = dbSelect.ToSql()
		if err != nil {
			return nil, errgo.Mask(err)
		}
	}
	_, err := db.Exec("CREATE TABLE `"+tableName+"` AS "+qry, args...)
	if err != nil {
		return nil, errgo.Mask(err)
	}

	return GetColumns(db, tableName)
}
Esempio n. 6
0
// LoadStringEntities executes a SELECT query and returns a slice containing columns names and its string values
func LoadStringEntities(db *sql.DB, dbSelect *dbr.SelectBuilder) ([]StringEntities, error) {

	qry, args, err := dbSelect.ToSql()
	if err != nil {
		return nil, errgo.Mask(err)
	}

	rows, err := db.Query(qry, args...)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	defer rows.Close()

	columnNames, err := rows.Columns()
	if err != nil {
		return nil, errgo.Mask(err)
	}

	ret := make([]StringEntities, 0, 2000)
	rss := newRowTransformer(columnNames)
	for rows.Next() {

		if err := rows.Scan(rss.cp...); err != nil {
			return nil, errgo.Mask(err)
		}
		err := rss.toString()
		if err != nil {
			return nil, errgo.Mask(err)
		}
		rss.append(&ret)
	}
	return ret, nil
}
Esempio n. 7
0
func (a *defaultAuthenticator) getUserInfo(r *http.Request, providerName string) (*authInfo, error) {
	provider, err := a.getAuthProvider(r, providerName)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	m := make(objx.Map)
	if r.Form == nil {
		r.ParseForm()
	}
	for k, v := range r.Form {
		m.Set(k, v)
	}
	creds, err := provider.CompleteAuth(m)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	user, err := provider.GetUser(creds)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	info := &authInfo{
		name:  user.Name(),
		email: user.Email(),
	}

	return info, nil
}
Esempio n. 8
0
// GetColumns returns all columns from a table. It discards the column
// entity_type_id from some entity tables. The column attribute_model will also
// be dropped from table eav_attribute
func GetColumns(db *sql.DB, table string) (Columns, error) {
	var cols = make(Columns, 0, 200)
	rows, err := db.Query("SHOW COLUMNS FROM `" + table + "`")
	if err != nil {
		return nil, errgo.Mask(err)
	}
	defer rows.Close()

	col := column{}
	for rows.Next() {

		err := rows.Scan(&col.Field, &col.Type, &col.Null, &col.Key, &col.Default, &col.Extra)
		if err != nil {
			return nil, errgo.Mask(err)
		}
		if isIgnoredColumn(table, col.Field.String) {
			continue
		}
		cols = append(cols, col)
	}
	err = rows.Err()
	if err != nil {
		return nil, errgo.Mask(err)
	}
	return cols, nil
}
Esempio n. 9
0
// getEntityTypeData retrieves all EAV models from table eav_entity_type but only those listed in variable
// codegen.ConfigEntityType. It then applies the mapping data from codegen.ConfigEntityType to the entity_type struct.
// Depends on generated code from tableToStruct.
func getEntityTypeData(dbrSess *dbr.Session) (etc eav.TableEntityTypeSlice, err error) {

	s, err := eav.TableCollection.Structure(eav.TableIndexEntityType)
	if err != nil {
		return nil, errgo.Mask(err)
	}

	_, err = dbrSess.
		Select(s.AllColumnAliasQuote(s.Name)...).
		From(s.Name).
		Where("entity_type_code IN ?", codegen.ConfigEntityType.Keys()).
		LoadStructs(&etc)
	if err != nil {
		return nil, errgo.Mask(err)
	}

	for typeCode, mapData := range codegen.ConfigEntityType {
		// map the fields from the config struct to the data retrieved from the database.
		et, err := etc.GetByCode(typeCode)
		codegen.LogFatal(err)
		et.EntityModel = codegen.ParseString(mapData.EntityModel, et)
		et.AttributeModel.String = codegen.ParseString(mapData.AttributeModel, et)
		et.EntityTable.String = codegen.ParseString(mapData.EntityTable, et)
		et.IncrementModel.String = codegen.ParseString(mapData.IncrementModel, et)
		et.AdditionalAttributeTable.String = codegen.ParseString(mapData.AdditionalAttributeTable, et)
		et.EntityAttributeCollection.String = codegen.ParseString(mapData.EntityAttributeCollection, et)
	}

	return etc, nil
}
Esempio n. 10
0
File: storage.go Progetto: fd/mauth
// LoadAccess retrieves access data by token. Client information MUST be loaded together.
// AuthorizeData and AccessData DON'T NEED to be loaded if not easily available.
// Optionally can return error if expired.
func (s *Storage) LoadAccess(token string) (*osin.AccessData, error) {
	var (
		data   *accessData
		client *Client
	)

	err := s.accessData.Find(bson.M{"accesstoken": token}).One(&data)
	if err == mgo.ErrNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, errgo.Mask(err)
	}

	err = s.clients.FindId(data.ClientID).One(&client)
	if err == mgo.ErrNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, errgo.Mask(err)
	}

	d := &osin.AccessData{
		Client:       client,
		AccessToken:  data.AccessToken,
		RefreshToken: data.RefreshToken,
		Scope:        data.Scope,
		RedirectUri:  data.RedirectUri,
		CreatedAt:    data.CreatedAt,
		ExpiresIn:    data.ExpiresIn,
		UserData:     data,
	}

	return d, nil
}
Esempio n. 11
0
func (d *defaultDataMapper) getPhotoDetail(photoID int64, user *user) (*photoDetail, error) {

	photo := &photoDetail{}

	if photoID == 0 {
		return photo, sql.ErrNoRows
	}

	q := "SELECT p.*, u.name AS owner_name " +
		"FROM photos p JOIN users u ON u.id = p.owner_id " +
		"WHERE p.id=$1"

	if err := d.SelectOne(photo, q, photoID); err != nil {
		return photo, errgo.Mask(err)
	}

	var tags []tag

	if _, err := d.Select(&tags,
		"SELECT t.* FROM tags t JOIN photo_tags pt ON pt.tag_id=t.id "+
			"WHERE pt.photo_id=$1", photo.ID); err != nil {
		return photo, errgo.Mask(err)
	}
	for _, tag := range tags {
		photo.Tags = append(photo.Tags, tag.Name)
	}

	photo.Permissions = &permissions{
		photo.canEdit(user),
		photo.canDelete(user),
		photo.canVote(user),
	}
	return photo, nil

}
Esempio n. 12
0
func (t *transaction) updateTags(photo *photo) error {

	var (
		args    = []string{"$1"}
		params  = []interface{}{interface{}(photo.ID)}
		isEmpty = true
		counter = 1
	)
	for _, name := range photo.Tags {
		name = strings.TrimSpace(name)
		if name != "" {
			counter++
			args = append(args, fmt.Sprintf("$%d", counter))
			params = append(params, interface{}(strings.ToLower(name)))
			isEmpty = false
		}
	}

	if isEmpty && photo.ID != 0 {
		_, err := t.Exec("DELETE FROM photo_tags WHERE photo_id=$1", photo.ID)
		return errgo.Mask(err)
	}
	if _, err := t.Exec(fmt.Sprintf("SELECT add_tags(%s)", strings.Join(args, ",")), params...); err != nil {
		return errgo.Mask(err)
	}
	return nil

}
Esempio n. 13
0
// WithIsCountryAllowedByIP a more advanced function. It expects from the context
// the store.ManagerReader ...
func (s *Service) WithIsCountryAllowedByIP() ctxhttp.Middleware {
	return func(h ctxhttp.Handler) ctxhttp.Handler {
		return ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

			_, requestedStore, err := store.FromContextReader(ctx)
			if err != nil {
				if PkgLog.IsDebug() {
					PkgLog.Debug("geoip.WithCountryByIP.FromContextManagerReader", "err", err)
				}
				return errgo.Mask(err)
			}

			var ipCountry *IPCountry
			ctx, ipCountry, err = s.newContextCountryByIP(ctx, r)
			if err != nil {
				ctx = NewContextWithError(ctx, err)
				return h.ServeHTTPContext(ctx, w, r)
			}

			allowedCountries, err := directory.AllowedCountries(requestedStore.Config)
			if err != nil {
				if PkgLog.IsDebug() {
					PkgLog.Debug("geoip.WithCountryByIP.directory.AllowedCountries", "err", err, "st.Config", requestedStore.Config)
				}
				return errgo.Mask(err)
			}

			if false == s.IsAllowed(requestedStore, ipCountry, allowedCountries, r) {
				h = s.altHandlerByID(requestedStore)
			}

			return h.ServeHTTPContext(ctx, w, r)
		})
	}
}
Esempio n. 14
0
func NewFioRunner(c FioConfiguration) (FioRunner, error) {
	var err error

	if !fioExists() {
		return FioRunner{}, errgo.Newf("Cannot locate fio. Looks like it is not installed on your system.")
	}

	if c.GenerateBandwithLogs || c.GenerateIOPSLogs || c.GenerateLatencyLogs {
		if len(c.LogsPrefix) < 1 {
			return FioRunner{}, errgo.Newf("You have to set a log file prefix")

		}
	}

	if c.JobDirectory, err = filepath.Abs(c.JobDirectory); err != nil {
		return FioRunner{}, errgo.Mask(err)
	}

	if c.WorkingDirectory, err = filepath.Abs(c.WorkingDirectory); err != nil {
		return FioRunner{}, errgo.Mask(err)
	}

	if c.LogsDirectory, err = filepath.Abs(c.LogsDirectory); err != nil {
		return FioRunner{}, errgo.Mask(err)
	}

	return FioRunner{
		conf: c,
	}, nil
}
Esempio n. 15
0
func (r FioRunner) createWorkingDirectory() error {
	f, err := os.Open(r.conf.WorkingDirectory)

	if err != nil {
		if os.IsNotExist(err) {
			os.Mkdir(r.conf.WorkingDirectory, 0755)
			return nil
		} else {
			return errgo.Mask(err)
		}
	}

	if fi, err := f.Stat(); err != nil {
		return errgo.Mask(err)
	} else if !fi.IsDir() {
		return errgo.Newf("Working directory '%s' exists but appears to be a file.", r.conf.WorkingDirectory)
	}

	if fis, err := f.Readdir(0); err != nil {
		return errgo.Mask(err)
	} else if len(fis) > 0 {
		return errgo.Newf("Working directory '%s' appears to be not empty.", r.conf.WorkingDirectory)
	}

	return nil
}
Esempio n. 16
0
// RemoveInstanceFromDiscovery removes an instance from etcd discovery.
func RemoveInstanceFromDiscovery(i swarmtypes.Instance) error {
	etcdMemberName, err := ssh.GetEtcd2MemberName(i.PublicIPAddress)
	if err != nil {
		return errgo.Mask(err)
	}
	// Not a quorum member
	if etcdMemberName == "" {
		return nil
	}

	discoveryUrl, err := ssh.GetEtcdDiscoveryUrl(i.PublicIPAddress)
	if err != nil {
		return errgo.Mask(err)
	}

	machineUrl := discoveryUrl + "/" + etcdMemberName
	req, err := http.NewRequest("DELETE", machineUrl, nil)
	if err != nil {
		return errgo.Mask(err)
	}

	_, err = http.DefaultClient.Do(req)
	if err != nil {
		return errgo.Mask(err)
	}
	return nil
}
Esempio n. 17
0
// Init initializes the appStore from a scope code and a scope type.
// This function is mainly used when booting the app to set the environment configuration
// Also all other calls to any method receiver with nil arguments depends on the appStore.
// @see \Magento\Store\Model\StorageFactory::_reinitStores
func (sm *Manager) Init(scopeCode config.ScopeIDer, scopeType config.ScopeGroup) error {
	if sm.appStore != nil {
		return ErrAppStoreSet
	}
	var err error
	switch scopeType {
	case config.ScopeStoreID:
		sm.appStore, err = sm.Store(scopeCode)
	case config.ScopeGroupID:
		g, errG := sm.Group(scopeCode) // this is the group_id
		if errG != nil {
			return errgo.Mask(errG)
		}
		sm.appStore, err = g.DefaultStore()
		break
	case config.ScopeWebsiteID:
		w, errW := sm.Website(scopeCode)
		if errW != nil {
			return errgo.Mask(errW)
		}
		sm.appStore, err = w.DefaultStore()
		break
	default:
		return ErrUnsupportedScopeGroup
	}
	return errgo.Mask(err)
}
Esempio n. 18
0
File: storage.go Progetto: fd/mauth
// LoadAuthorize looks up AuthorizeData by a code.
// Client information MUST be loaded together.
// Optionally can return error if expired.
func (s *Storage) LoadAuthorize(code string) (*osin.AuthorizeData, error) {
	var (
		mdata  authorizeData
		client *Client
	)

	err := s.authData.Find(bson.M{"code": code}).One(&mdata)
	if err == mgo.ErrNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, errgo.Mask(err)
	}

	err = s.clients.FindId(mdata.ClientID).One(&client)
	if err == mgo.ErrNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, errgo.Mask(err)
	}

	data := &osin.AuthorizeData{
		Client:      client,
		Code:        mdata.Code,
		ExpiresIn:   mdata.ExpiresIn,
		Scope:       mdata.Scope,
		RedirectUri: mdata.RedirectUri,
		State:       mdata.State,
		CreatedAt:   mdata.Created,
	}

	return data, nil
}
Esempio n. 19
0
// InitByRequest returns a new Store read from a cookie or HTTP request param.
// The internal appStore must be set before hand.
// 1. check cookie store, always a string and the store code
// 2. check for ___store variable, always a string and the store code
// 3. May return nil,nil if nothing is set.
// This function must be used within an HTTP handler.
// The returned new Store must be used in the HTTP context and overrides the appStore.
// @see \Magento\Store\Model\StorageFactory::_reinitStores
func (sm *Manager) InitByRequest(res http.ResponseWriter, req *http.Request, scopeType config.ScopeGroup) (*Store, error) {
	if sm.appStore == nil {
		// that means you must call Init() before executing this function.
		return nil, ErrAppStoreNotSet
	}

	var reqStore *Store
	if keks := GetCodeFromCookie(req); keks != nil {
		reqStore, _ = sm.GetRequestStore(keks, scopeType) // ignore errors
	}

	if reqStoreCode := req.URL.Query().Get(HTTPRequestParamStore); reqStoreCode != "" {
		var err error
		// @todo reqStoreCode if number ... cast to int64 because then group id if ScopeGroup is group.
		if reqStore, err = sm.GetRequestStore(config.ScopeCode(reqStoreCode), scopeType); err != nil {
			return nil, errgo.Mask(err)
		}
		// also delete and re-set a new cookie
		if reqStore != nil && reqStore.Data().Code.String == reqStoreCode {
			wds, err := reqStore.Website().DefaultStore()
			if err != nil {
				return nil, errgo.Mask(err)
			}
			if wds.Data().Code.String == reqStoreCode {
				reqStore.DeleteCookie(res) // cookie not needed anymore
			} else {
				reqStore.SetCookie(res) // make sure we force set the new store
			}
		}
	}
	return reqStore, nil // can be nil,nil
}
Esempio n. 20
0
// Init loads the column definitions from the database for each table. Set reInit
// to true to allow reloading otherwise it loads only once.
func (tm *TableManager) Init(dbrSess dbr.SessionRunner, reInit ...bool) error {
	reLoad := false
	if len(reInit) > 0 {
		reLoad = reInit[0]
	}
	if true == tm.initDone && false == reLoad {
		return nil
	}
	if false == tm.initDone && true == reLoad {
		return errgo.Mask(ErrManagerInitReload)
	}
	tm.mu.Lock()
	defer tm.mu.Unlock()
	tm.initDone = true

	for _, table := range tm.ts {
		if err := table.LoadColumns(dbrSess); err != nil {
			if PkgLog.IsDebug() {
				PkgLog.Debug("csdb.TableManager.Init.LoadColumns", "err", err, "table", table)
			}
			return errgo.Mask(err)
		}
	}

	return nil
}
Esempio n. 21
0
func parseCloudformationTemplate(templatePath string, cfg interface{}) (string, error) {
	f, err := os.Create(generatedCloudformationPath)
	if err != nil {
		return generatedCloudformationPath, errgo.Mask(err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()

	absoluteCloudFormationTemplatePath, err := filepath.Abs(templatePath)
	if err != nil {
		return "", errgo.Mask(err)
	}

	templateData, err := ioutil.ReadFile(absoluteCloudFormationTemplatePath)
	if err != nil {
		return "", errgo.Mask(err)
	}

	var tmpl *template.Template
	if tmpl, err = template.New("cloudformation").Parse(string(templateData)); err != nil {
		return generatedCloudformationPath, errgo.Mask(err)
	}

	if err = tmpl.Execute(f, cfg); err != nil {
		return generatedCloudformationPath, errgo.Mask(err)
	}

	return generatedCloudformationPath, nil
}
Esempio n. 22
0
func (*errorsSuite) TestMask(c *gc.C) {
	err0 := errgo.WithCausef(nil, someErr, "foo") //err TestMask#0
	err := errgo.Mask(err0)                       //err TestMask#1
	checkErr(c, err, err0, "foo", "[{$TestMask#1$: } {$TestMask#0$: foo}]", err)

	err = errgo.Mask(nil)
	c.Assert(err, gc.IsNil)
}
Esempio n. 23
0
func TestMask(t *testing.T) {
	err0 := errgo.WithCausef(nil, someErr, "foo") //err TestMask#0
	err := errgo.Mask(err0)                       //err TestMask#1
	checkErr(t, err, err0, "foo", "[{$TestMask#1$: } {$TestMask#0$: foo}]", err)

	err = errgo.Mask(nil)
	if err != nil {
		t.Fatalf("expected nil got %#v", err)
	}
}
Esempio n. 24
0
func (et *TableEntityType) LoadByCode(dbrSess *dbr.Session, code string, cbs ...csdb.DbrSelectCb) error {
	s, err := TableCollection.Structure(TableIndexEntityType)
	if err != nil {
		return errgo.Mask(err)
	}
	sb := dbrSess.Select(s.AllColumnAliasQuote(csdb.MainTable)...).From(s.Name, csdb.MainTable).Where("entity_type_code = ?", code)
	for _, cb := range cbs {
		sb = cb(sb)
	}
	return errgo.Mask(sb.LoadStruct(et))
}
Esempio n. 25
0
func (d *defaultDataMapper) updateTags(photo *photo) error {
	tx, err := d.begin()
	if err != nil {
		return errgo.Mask(err)
	}
	if err := tx.updateTags(photo); err != nil {
		tx.Rollback()
		return err
	}
	return errgo.Mask(tx.Commit())
}
Esempio n. 26
0
// WithValidateBaseURL is a middleware which checks if the request base URL
// is equal to the one store in the configuration, if not
// i.e. redirect from http://example.com/store/ to http://www.example.com/store/
// @see app/code/Magento/Store/App/FrontController/Plugin/RequestPreprocessor.php
func WithValidateBaseURL(cg config.GetterPubSuber) ctxhttp.Middleware {

	// Having the GetBool command here, means you must restart the app to take
	// changes in effect. @todo refactor and use pub/sub to automatically change
	// the isRedirectToBase value.

	// <todo check logic!>
	cgDefaultScope := cg.NewScoped(0, 0, 0)
	configRedirectCode := backend.Backend.WebURLRedirectToBase.Get(cgDefaultScope)

	redirectCode := http.StatusMovedPermanently
	if configRedirectCode != redirectCode {
		redirectCode = http.StatusFound
	}
	// </todo check logic>

	return func(hf ctxhttp.HandlerFunc) ctxhttp.HandlerFunc {
		return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

			if configRedirectCode > 0 && r.Method != "POST" {

				_, requestedStore, err := FromContextReader(ctx)
				if err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("ctxhttp.WithValidateBaseUrl.FromContextServiceReader", "err", err, "ctx", ctx)
					}
					return errgo.Mask(err)
				}

				baseURL, err := requestedStore.BaseURL(config.URLTypeWeb, requestedStore.IsCurrentlySecure(r))
				if err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("ctxhttp.WithValidateBaseUrl.requestedStore.BaseURL", "err", err, "ctx", ctx)
					}
					return errgo.Mask(err)
				}

				if err := httputil.IsBaseURLCorrect(r, &baseURL); err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("store.WithValidateBaseUrl.IsBaseUrlCorrect.error", "err", err, "baseURL", baseURL, "request", r)
					}

					baseURL.Path = r.URL.Path
					baseURL.RawPath = r.URL.RawPath
					baseURL.RawQuery = r.URL.RawQuery
					baseURL.Fragment = r.URL.Fragment
					http.Redirect(w, r, (&baseURL).String(), redirectCode)
					return nil
				}
			}
			return hf(ctx, w, r)
		}
	}
}
Esempio n. 27
0
func Connect() (*sql.DB, *dbr.Connection, error) {
	dsn, err := GetDSN()
	if err != nil {
		return nil, nil, errgo.Mask(err)
	}
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		return nil, nil, errgo.Mask(err)
	}
	dbrConn := dbr.NewConnection(db, nil)
	return db, dbrConn, nil
}
Esempio n. 28
0
func (a *defaultAuthenticator) getRedirectURL(r *http.Request, providerName string) (string, error) {
	provider, err := a.getAuthProvider(r, providerName)
	if err != nil {
		return "", errgo.Mask(err)
	}
	state := gomniauth.NewState("after", "success")
	url, err := provider.GetBeginAuthURL(state, nil)
	if err != nil {
		return url, errgo.Mask(err)
	}
	return url, nil
}
Esempio n. 29
0
// WithValidateBaseURL is a middleware which checks if the request base URL
// is equal to the one store in the configuration, if not
// i.e. redirect from http://example.com/store/ to http://www.example.com/store/
// @see app/code/Magento/Store/App/FrontController/Plugin/RequestPreprocessor.php
func WithValidateBaseURL(cr config.ReaderPubSuber) ctxhttp.Middleware {

	// Having the GetBool command here, means you must restart the app to take
	// changes in effect. @todo refactor and use pub/sub to automatically change
	// the isRedirectToBase value.
	checkBaseURL, err := cr.GetBool(config.Path(PathRedirectToBase)) // scope default
	if config.NotKeyNotFoundError(err) && PkgLog.IsDebug() {
		PkgLog.Debug("ctxhttp.WithValidateBaseUrl.GetBool", "err", err, "path", PathRedirectToBase)
	}

	redirectCode := http.StatusMovedPermanently
	if rc, err := cr.GetInt(config.Path(PathRedirectToBase)); rc != redirectCode && false == config.NotKeyNotFoundError(err) {
		redirectCode = http.StatusFound
	}

	return func(h ctxhttp.Handler) ctxhttp.Handler {
		return ctxhttp.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

			if checkBaseURL && r.Method != "POST" {

				_, requestedStore, err := FromContextReader(ctx)
				if err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("ctxhttp.WithValidateBaseUrl.FromContextServiceReader", "err", err, "ctx", ctx)
					}
					return errgo.Mask(err)
				}

				baseURL, err := requestedStore.BaseURL(config.URLTypeWeb, requestedStore.IsCurrentlySecure(r))
				if err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("ctxhttp.WithValidateBaseUrl.requestedStore.BaseURL", "err", err, "ctx", ctx)
					}
					return errgo.Mask(err)
				}

				if err := httputils.IsBaseURLCorrect(r, &baseURL); err != nil {
					if PkgLog.IsDebug() {
						PkgLog.Debug("store.WithValidateBaseUrl.IsBaseUrlCorrect.error", "err", err, "baseURL", baseURL, "request", r)
					}

					baseURL.Path = r.URL.Path
					baseURL.RawPath = r.URL.RawPath
					baseURL.RawQuery = r.URL.RawQuery
					baseURL.Fragment = r.URL.Fragment
					http.Redirect(w, r, (&baseURL).String(), redirectCode)
					return nil
				}
			}
			return h.ServeHTTPContext(ctx, w, r)
		})
	}
}
Esempio n. 30
0
// FindGroupByPath searches for a field using the all three path segments.
// If one argument is given then considered as the full path e.g. a/b/c
// If three arguments are given then each argument will be treated as a path part.
func (ss SectionSlice) FindFieldByPath(paths ...string) (*Field, error) {
	if len(paths) == 1 {
		paths = scope.PathSplit(paths[0])
	}
	if len(paths) < 3 {
		return nil, errgo.Mask(ErrFieldNotFound)
	}
	cg, err := ss.FindGroupByPath(paths...)
	if err != nil {
		return nil, errgo.Mask(err)
	}
	return cg.Fields.FindByID(paths[2])
}