Why are GCP Cloudbalancer Bucket returning a XML? - gcp-load-balancer

Recently, I created a bucket with an index.html and configured a loadbalancer http with bucket as backend.
I have been made this some times and it always has been worked, but in my last configuration it is returning an XML with this structure:
<ListBucketResult>
<Name>bucket_name</Name>
<Prefix/>
<Marker/>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>01.png</Key>
<Generation>1587402672429680</Generation>
<MetaGeneration>1</MetaGeneration>
<LastModified>2020-04-20T17:11:12.429Z</LastModified>
<ETag>"0a3750e6fdb29fcd632e627e2fc34dde"</ETag>
<Size>211</Size>
</Contents>
...
I have been changed a lot of stuff and it doesn't seem work, what could be wrong in this configuration? some clues?

You need to declare the main page and the error page in google cloud
example by gsuit:
gsutil web set -m index.html -e index.html gs://www.example.com
More information here

Related

Laravel forcing Http for asssets

this is a little bit strange because most of the questions here wanted to force https.
While learning AWS elastic beanstalk. I am hosting a laravel site there. Everything is fine, except that none of my javascripts and css files are being loaded.
If have referenced them in the blade view as :
<script src="{{asset('assets/backend/plugins/jquery/jquery.min.js')}}"></script>
First thing I tried was looking into the file/folder permissions in the root of my project by SSHing into EC2 instance. Didn't work even when I set the permission to public folder to 777.
Later I found out that, the site's main page url was http while all the assets url were 'https'.
I dont want to get into the SSL certificates things just yet, if it is possible.
Is there anyway I can have my assets url be forced to Http only?
Please forgive my naiveity. Any help would be appreciated.
This usually happens if your site is for example behind an reverse proxy, As the URL helper facade, trusts on your local instance that is beyond the proxy, and might not use SSL. Which can be misleading/wrong.
Which is probaly the case on a EC2 instance... as the SSL termination is beyond load balancers/HA Proxies.
i usually add the following to my AppServiceProvider.php
public function boot()
{
if (Str::startsWith(config('app.url'), 'https')) {
\URL::forceScheme('https');
} else {
\URL::forceScheme('http');
}
}
Of course this needs to ensure you've set app.url / APP_URL, if you are not using that, you can just get rid of the if statement. But is a little less elegant, and disallows you to develop on non https

How to make self hosted prebid server work with AMP?

The github repo does not mention any thing on how to use the self hosted server work with AMP . They have given an endpoint to access after storing the request in the server:
/openrtb2/amp?tag_id={ID} //id is the file name of request stored.
I do get the empty targeting response(since server is on local setup).But how will this data reach the amp page? where should this endpoint be requested?
I read in AMP docs that user generated js doesn't work in AMP. I also setup rtc config on amp ad tag and adding vendor as per vendor config(callout-vendor.js)which sends request to the bidder's prebid server url. For example:
If i add appnexus in rtc-config vendors, the request is getting sent to:
https://prebid.adnxs.com/pbs/v1/openrtb2/amp?tag_id={id of tag}
How do i make the request to go to self hosted prebid server url
ok, figured this thing out..
Doc reference: https://github.com/ampproject/amphtml/blob/master/extensions/amp-a4a/rtc-publisher-implementation-guide.md#setting-up-rtc-config
So, first we make an amp-ad tag wherein we have rtc-config attribute. Since, we are using our own server, we'll need to add it in url property as mentioned in that example in doc.
<amp-ad width="320" height="50"
type="network-foo"
data-slot="/1234/5678"
rtc-config={"urls":["our-server-url"]}>
</amp-ad>
The targeting data returned by server will be fetched by amp-ad tag and will be appended to adserver request.
Additionally, we can make a pull request to amphtml repo on github to add our server url in callout-vendor.js file
file reference: https://github.com/ampproject/amphtml/blob/master/extensions/amp-a4a/0.1/callout-vendors.js
Then, the amp-ad tag will look like this:
<amp-ad width="320" height="50"
type="network-foo"
data-slot="/1234/5678"
rtc-config={"vendors": {"serverAliasAsSetInCalloutVendorFile": {"MACRO1": "MacroValue"}}>
</amp-ad>

Serve static files in Flask from private AWS S3 bucket

I am developing a Flask app running on Heroku that allows users to upload images. The app has a page displaying the user's images in a table.
For developing purposes, I am saving the uploaded files to Heroku's ephemeral file system, and everything works fine: the images are correctly loaded and displayed (I am using the last method shown here implying the use of send_from_directory()). Now I have moved the storage to S3 and I am trying to adapt the code. I use boto3 to upload the files to the bucket: it works fine. My doubts are related to the download to populate the users' pages with their images.
As explained here, I could set the file as "public-read" and use the URL (I think this is what Flask-S3 does), but I'd rather prefer not to leave free access to the files. So, my solution attempt is to download the file to Heroku's filesystem and serve the image using again the send_from_directory() as follows:
app.py
#app.route('/download/<resource>')
def download_image(resource):
""" resource: name of the file to download"""
s3 = boto3.client('s3',
aws_access_key_id=current_app.config['S3_ACCESS_KEY'],
aws_secret_access_key=current_app.config['S3_SECRET_KEY'])
s3.download_file(current_app.config['S3_BUCKET_NAME'],
resource,
os.path.join('tmp',
resource))
return send_from_directory('tmp', # Heroku's filesystem
resource,
as_attachment=False)
Then, in the template I generate the URL for the image as follows:
...
<img src="{{ url_for('app.download_image',
resource=resource) }}" height="120" width="120">
...
It works, but I don't think this is the proper way for some reasons: among them, I should manage the Heroku's filesystem to avoid using up all the space between dynos restart (I should delete the images from the filesystem).
Which is the best/preferred way, also considering the performance?
Thanks a lot
The preferred way is to simply create a pre-signed URL for the image, and return a redirect to that URL. This keeps the files private in S3, but generates a temporary, time limited, URL that can be used to download the file directly from S3. That will greatly reduce the amount of work happening on your server, as well as the amount of data transfer being consumed by your server. Something like this:
#app.route('/download/<resource>')
def download_image(resource):
""" resource: name of the file to download"""
s3 = boto3.client('s3',
aws_access_key_id=current_app.config['S3_ACCESS_KEY'],
aws_secret_access_key=current_app.config['S3_SECRET_KEY'])
url = s3.generate_presigned_url('get_object', Params = {'Bucket': 'S3_BUCKET_NAME', 'Key': resource}, ExpiresIn = 100)
return redirect(url, code=302)
If you don't like that solution, you should at least look into streaming the file contents from S3 instead of writing it to the file system.

Swagger page being redirected from https to http

AWS Elastic Load Balancer listening through HTTPS (443) using SSL and redirecting requests to EC2 instances through HTTP (80), with IIS hosting a .net webapi application, using swashbuckle to describe the API methods.
Home page of the API (https://example.com) has a link to Swagger documentation which can bee read as https://example.com/swagger/ui/index.html when you hove over on the link.
If I click on the link it redirects the request on the browser to http://example.com/swagger/ui/index.html which displays a Page Not Found error
but if I type directly in the browser URL https://example.com/swagger/ui/index.html then it loads Swagger page, but then, when expanding the methods an click on "Try it out", the Request URL starts with "http" again.
This configuration is only for Stage and Production environments. Lower environments don't use the load balancer and just use http.
Any ideas on how to stop https being redirected to http? And how make swagger to display Request URLs using https?
Thank you
EDIT:
I'm using a custom index.html file
Seems is a known issue for Swashbuckle. Quote:
"By default, the service root url is inferred from the request used to access the docs. However, there may be situations (e.g. proxy and load-balanced environments) where this does not resolve correctly. You can workaround this by providing your own code to determine the root URL."
What I did was provide the root url and/or scheme to use based on the environment
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
...
c.RootUrl(req => GetRootUrlFromAppConfig(req));
...
c.Schemes(GetEnvironmentScheme());
...
})
.EnableSwaggerUi(c =>
{
...
});
where
public static string[] GetEnvironmentScheme()
{
...
}
public static string GetRootUrlFromAppConfig(HttpRequestMessage request)
{
...
}
The way I would probably do it is having a main file, and generating during the build of your application a different swagger file based on the environnement parameters for schemes and hosts.
That way, you have to manage only one swagger file accross your environments, and you only have to manage a few extra environnement properties, host and schemes (if you don't already have them)
Since I don't know about swashbuckle, I cannot answer for sure at your first question (the redirect)

Redirecting to API/Docs/index.html folder in Lumen

So i have an API created with Lumen with some documentation done with Apidoc outside of the public folder and i'd like to serve it when the user goes to the URL http://apidomain.com/docs
This is the structure of the app
ProjectRoot
->API
->Auth
->Docs
->v1
->app
->bootstrap
->database
->public
...
Is there any way to create a route that sends the user to API/Docs?
It's done, it was actually my bad, when trying to call the file via routes it actually messed up the filepath for the other files. So when i looked in the dev tools on chrome i noticed i was getting 404's on my js and css files, hence the failure to load the ApiDoc

Resources