Пример #1
0
func (c *CloudflareProvider) Init(rootDomainName string) error {
	var email, apiKey string
	if email = os.Getenv("CLOUDFLARE_EMAIL"); len(email) == 0 {
		return fmt.Errorf("CLOUDFLARE_EMAIL is not set")
	}

	if apiKey = os.Getenv("CLOUDFLARE_KEY"); len(apiKey) == 0 {
		return fmt.Errorf("CLOUDFLARE_KEY is not set")
	}

	c.client = api.New(&api.Options{
		Email: email,
		Key:   apiKey,
	})

	c.ctx = context.Background()
	c.root = utils.UnFqdn(rootDomainName)

	if err := c.setZone(); err != nil {
		return fmt.Errorf("Failed to set zone for root domain %s: %v", c.root, err)
	}

	logrus.Infof("Configured %s with zone '%s'", c.GetName(), c.root)
	return nil
}
Пример #2
0
func init() {
	cloudflareHandler := &CloudflareHandler{}

	email := os.Getenv("CLOUDFLARE_EMAIL")
	if len(email) == 0 {
		logrus.Infof("CLOUDFLARE_EMAIL is not set, skipping init of %s provider", cloudflareHandler.GetName())
		return
	}

	apiKey := os.Getenv("CLOUDFLARE_KEY")
	if len(apiKey) == 0 {
		logrus.Infof("CLOUDFLARE_KEY is not set, skipping init of %s provider", cloudflareHandler.GetName())
		return
	}

	if err := RegisterProvider("cloudflare", cloudflareHandler); err != nil {
		logrus.Fatal("Could not register cloudflare provider")
	}

	cloudflareHandler.client = cloudflare.New(&cloudflare.Options{
		Email: email,
		Key:   apiKey,
	})

	cloudflareHandler.ctx = context.Background()
	cloudflareHandler.root = unFqdn(dns.RootDomainName)

	if err := cloudflareHandler.setZone(); err != nil {
		logrus.Fatalf("Failed to set zone for root domain %s: %v", cloudflareHandler.root, err)
	}

	logrus.Infof("Configured %s with zone \"%s\" ", cloudflareHandler.GetName(), cloudflareHandler.root)
}
Пример #3
0
func main() {
	flag.Parse()

	exip, err := metadata.ExternalIP()
	if err != nil {
		log.Fatal(err)
	}

	client := cloudflare.New(&cloudflare.Options{
		Email: *user,
		Key:   *key,
	})

	ctx := context.Background()
	ctx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second*30))

	zones, err := client.Zones.List(ctx)
	if err != nil {
		log.Fatal(err)
	} else if len(zones) == 0 {
		log.Fatal("No zones were found")
	} else if len(zones) != 1 {
		log.Fatal("More than one zone found?")
	}

	if zones[0].Name != "nella.org" {
		log.Fatal("not nella.org?")
	}

	records, err := client.Records.List(ctx, zones[0].ID)
	if err != nil {
		log.Fatal(err)
	}

	// remove all existing ns.nella.org records
	for _, record := range records {
		if record.Name == "ns.nella.org" {
			log.Print("delete ", record.Content)
			err = client.Records.Delete(ctx, record.ZoneID, record.ID)
			if err != nil {
				log.Fatal("delete: ", err)
			}
		}
	}

	rec := &cloudflare.Record{
		Type:    "A",
		Name:    "ns.nella.org",
		Content: exip,
		TTL:     120,
		ZoneID:  zones[0].ID,
	}
	log.Print("create: ", exip)
	err = client.Records.Create(ctx, rec)
	if err != nil {
		log.Fatal("create: ", err)
	}
}
Пример #4
0
// Client() returns a new client for accessing cloudflare.
func (c *Config) Client() (*cloudflare.Client, error) {
	client := cloudflare.New(&cloudflare.Options{
		Email: c.Email,
		Key:   c.Token,
	})

	log.Printf("[INFO] CloudFlare Client configured for user: %s", c.Email)

	return client, nil
}
Пример #5
0
func client(c *cli.Context) *cloudflare.Client {
	opts := &cloudflare.Options{
		Key:   c.GlobalString("key"),
		Email: c.GlobalString("email"),
	}
	if opts.Key == "" || opts.Email == "" {
		log.Println("You have to provide Cloudflare Email and API key.")
		log.Println("Use CLOUDFLARE_EMAIL and CLOUDFLARE_KEY environment variables.")
		log.Println("Or alternatively provide them in -email and -key flags in each call.")
		os.Exit(255)
	}
	return cloudflare.New(opts)
}
Пример #6
0
// NewDNSProviderCloudFlare returns a DNSProviderCloudFlare instance with a configured cloudflare client.
// Authentication is either done using the passed credentials or - when empty - using the environment
// variables CLOUDFLARE_EMAIL and CLOUDFLARE_API_KEY.
func NewDNSProviderCloudFlare(cloudflareEmail, cloudflareKey string) (*DNSProviderCloudFlare, error) {
	if cloudflareEmail == "" || cloudflareKey == "" {
		cloudflareEmail, cloudflareKey = envAuth()
		if cloudflareEmail == "" || cloudflareKey == "" {
			return nil, fmt.Errorf("CloudFlare credentials missing")
		}
	}

	c := &DNSProviderCloudFlare{
		client: cloudflare.New(&cloudflare.Options{cloudflareEmail, cloudflareKey}),
		ctx:    context.Background(),
	}

	return c, nil
}
Пример #7
0
func (api *CloudFlareDNS) client() *cloudflare.Client {
	api.mutex.Lock()
	defer api.mutex.Unlock()

	if api._client != nil {
		return api._client
	}

	options := cloudflare.Options{
		Email: api.Email,
		Key:   api.Token,
	}
	api._client = cloudflare.New(&options)
	return api._client
}