Getting into openstack dev using this link, and turns out my auth_url is of the form http://192.168.43.18/identity/v3 from the openstack RC yaml file. When I use it, as used in the link, I get the following error message:
Traceback (most recent call last):
File "conn_tester.py", line 22, in <module>
images = conn.list_images()
File "/usr/local/lib/python2.7/dist-packages/libcloud/compute/drivers/openstack.py", line 282, in list_images
self.connection.request('/images/detail').object, ex_only_active)
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/openstack.py", line 223, in request
raw=raw)
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/base.py", line 536, in request
action = self.morph_action_hook(action)
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/openstack.py", line 290, in morph_action_hook
self._populate_hosts_and_request_paths()
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/openstack.py", line 324, in _populate_hosts_and_request_paths
osa = osa.authenticate(**kwargs) # may throw InvalidCreds
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/openstack_identity.py", line 855, in authenticate
return self._authenticate_2_0_with_password()
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/openstack_identity.py", line 880, in _authenticate_2_0_with_password
return self._authenticate_2_0_with_body(reqbody)
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/openstack_identity.py", line 885, in _authenticate_2_0_with_body
method='POST')
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/base.py", line 637, in request
response = responseCls(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/libcloud/common/base.py", line 157, in __init__
message=self.parse_error())
libcloud.common.exceptions.BaseHTTPError: {"error": {"message": "get_version_v3() got an unexpected keyword argument 'auth'", "code": 400, "title": "Bad Request"}}
I have tried changing the auth url to http://192.168.43.18:35357 and also port 5000, but I get this error:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.43.18', port=35357): Max retries exceeded with url: /v2.0/tokens (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fe06f7dff90>: Failed to establish a new connection: [Errno 111] Connection refused',))
Using devstack version 16.0.0
Python code:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
auth_username = 'demo'
auth_password = 'password'
#auth_url = 'http://controller:5000'
auth_url = 'http://192.168.43.18:35357'
#auth_url = 'http://192.168.43.18/identity/v3/'
project_name = 'demo'
region_name = 'RegionOne'
provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
auth_password,
ex_force_auth_url=auth_url,
ex_force_auth_version='2.0_password',
ex_tenant_name=project_name,
ex_force_service_region=region_name)
#print "hello"
images = conn.list_images()
for image in images:
print(image)
It looks like you could specify v3 instead:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
auth_username = 'demo'
auth_password = 'password'
auth_url = 'http://192.168.43.18:5000'
project_name = 'demo'
region_name = 'RegionOne'
provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
auth_password,
ex_force_auth_url=auth_url,
ex_force_auth_version='3.x_password',
ex_tenant_name=project_name,
ex_force_service_region=region_name)
#print "hello"
images = conn.list_images()
for image in images:
print(image)
There aren't many examples using v3. A tenant would normally be associated with 2.0, so I'm not sure if the ex_tenant_name option is required.
Keystone versioning:
https://developer.openstack.org/api-ref/identity/v3/
More information on libcloud:
https://libcloud.readthedocs.io/en/latest/compute/drivers/openstack.html#connecting-to-the-openstack-installation
Also, you can see if keystone is using v2.0:
curl http://192.168.43.18:5000/v2.0
If it is, then I would assume something like this would work:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
auth_username = 'demo'
auth_password = 'password'
auth_url = 'http://192.168.43.18:5000'
project_name = 'demo'
region_name = 'RegionOne'
provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
auth_password,
ex_force_auth_url=auth_url,
ex_force_auth_version='2.0_password',
ex_tenant_name=project_name,
ex_force_service_region=region_name)
#print "hello"
images = conn.list_images()
for image in images:
print(image)
If it still isn't working, verify that you are using the public keystone endpoint. Depending on your version, you may receive a different level of information:
openstack endpoint list --long
or
openstack endpoint list
Related
So, I created the following HTTP server tunneled via ngrok, and I am trying to send a file to the server, to then read it and display it on the web page of the server.
Here's the code for the server:
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
from pyngrok import ngrok
import time
port = os.environ.get("PORT", 80)
server_address = ("127.0.0.1", port)
class MyServer(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
def do_POST(self):
'''Reads post request body'''
self._set_headers()
content_len = int(self.headers.getheader('content-length', 0))
post_body = self.rfile.read(content_len)
self.wfile.write("received post request:<br>{}".format(post_body))
def do_PUT(self):
self.do_POST()
httpd = HTTPServer(server_address, MyServer)
public_url = ngrok.connect(port).public_url
print("ngrok tunnel \"{}\" -> \"http://127.0.0.1:{}\"".format(public_url, port))
try:
# Block until CTRL-C or some other terminating event
httpd.serve_forever()
except KeyboardInterrupt:
print(" Shutting down server.")
httpd.socket.close()
And I have been trying to send a file using POST as follow
>>> url = 'https://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
I imported requests of course, and here's what I get
Exception occurred during processing of request from ('127.0.0.1', 60603)
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\socketserver.py", line 316, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Program Files\Python39\lib\socketserver.py", line 347, in process_request
self.finish_request(request, client_address)
File "C:\Program Files\Python39\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Program Files\Python39\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Program Files\Python39\lib\http\server.py", line 427, in handle
self.handle_one_request()
File "C:\Program Files\Python39\lib\http\server.py", line 415, in handle_one_request
method()
File "C:\Users\pierr\OneDrive\Desktop\SpyWare-20210104T124335Z-001\SpyWare\Ngrok_Test.py", line 28, in do_POST
content_len = int(self.headers.getheader('content-length', 0))
AttributeError: 'HTTPMessage' object has no attribute 'getheader'
Could someone please help me fix this error ? I don't get where it comes from.
Syntax has changed. You need to use
content_len = int(self.headers.get('Content-Length'))
Instead of
content_len = int(self.headers.getheader('content-length', 0))
The rest should be the same
How to use senlinclient with keystone session? Can anyone give me an example? Thanks.
from keystoneauth1 import session
from keystoneauth1.identity import v3
from senlinclient.client import Client as senlinClient
def get_senlin_client_by_session(session):
return senlinClient(api_ver=1, session=session)
admin_auth = v3.Password(username='admin',
password='xxxxxx',
auth_url='http://vip:5000/v3',
project_name='admin',
user_domain_name='Default',
project_domain_name='Default'
)
session = session.Session(auth=admin_auth)
senlin_client = get_senlin_client_by_session(session)
print(senlin_client)
I tried the above example, but the following error will be reported.
File "/usr/lib/python2.7/site-packages/senlinclient/client.py", line 23, in Client
return cls(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/senlinclient/v1/client.py", line 28, in __init__
self.service = self.conn.cluster
File "/usr/lib/python2.7/site-packages/openstack/service_description.py", line 95, in __get__
allow_version_hack=True,
File "/usr/lib/python2.7/site-packages/openstack/config/cloud_region.py", line 457, in get_session_client
session=self.get_session(),
File "/usr/lib/python2.7/site-packages/openstack/config/cloud_region.py", line 324, in get_session
"Problem with auth parameters")
openstack.exceptions.ConfigException: Problem with auth parameters
The problem has been solved.
openstack rocky uses python2-senlinclient-1.8.0-1.el7.noarch and python2-openstacksdk-0.17.3-1.el7.noarch. I upgraded python2-senlinclient to version 1.9.0 and the problem was solved.
I am trying to access my coinbase account using their API.
I am using the following code: (which is from the website https://developers.coinbase.com/docs/wallet/api-key-authentication)
import json, hmac, hashlib, time, requests
from requests.auth import AuthBase
# Before implementation, set environmental variables with the names API_KEY and API_SECRET
API_KEY = 'API_KEY'
API_SECRET = 'API_SECRET'
# Create custom authentication for Coinbase API
class CoinbaseWalletAuth(AuthBase):
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def __call__(self, request):
timestamp = str(int(time.time()))
message = timestamp + request.method + request.path_url + (request.body or '')
signature = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
})
return request
api_url = 'https://api.coinbase.com/v2/'
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
# Get current user
r = requests.get(api_url + 'user', auth=auth)
print r.json()
# {u'data': {u'username': None, u'resource': u'user', u'name': u'User'...
However I am getting the following error:
Traceback (most recent call last):
File "test1.py", line 44, in <module>
r = requests.get(api_url + 'user', auth=auth)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\sessions.py", line 494, in request
prep = self.prepare_request(req)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\models.py", line 309, in prepare
self.prepare_auth(auth, url)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\models.py", line 540, in prepare_auth
r = auth(self)
File "test1.py", line 29, in __call__
signature = hmac.new(self.secret_key, message, hashlib.sha256).encode("utf-8").digest()
File "C:\Users\lclar\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 144, in new
return HMAC(key, msg, digestmod)
File "C:\Users\lclar\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 42, in __init__
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'
Can someone please help me?
Thanks in advance.
I assuming the data type self.secret_key = secret_key is a string. For Python >= 3.4, hmac.new(key, msg=None, digestmod=''), the key must be of type bytes or bytearray per the docs: https://docs.python.org/3/library/hmac.html
Likewise to avoid the TypeError: Unicode-objects must be encoded before hashing error, do the same for the message variable as shown below:
signature = hmac.new(self.secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()
I have a local install on openstack on my virtual box. I am trying to use the lib cloud api to connnect and get a list of images,flavours etc.
Below is the code that I am trying to execute
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
# Authentication information so you can authenticate to DreamCompute
# copy the details from the OpenStack RC file
# https://dashboard.dreamcompute.com/project/access_and_security/api_access/openrc/
auth_username = 'admin'
auth_password = 'f882e2f4eaad434c'
TENANT_NAME = 'admin'
project_name = 'admin'
auth_url = 'http://192.168.56.101:5000/v3/tokens'
region_name = 'RegionOne'
provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
auth_password,
ex_force_auth_url=auth_url,
ex_force_auth_version='2.0_password',
ex_tenant_name=project_name,
ex_force_service_type='compute',
ex_force_service_name='compute',
ex_force_base_url='http://192.168.56.101:8774/v2.1/29a8949bc3a04bfead0654be8e552017')
# Get the image that we want to use by its id
# NOTE: the image_id may change. See the documentation to find
# all the images available to your user
image_id = '4525d442-e9f4-4d19-889f-49ab03be93df'
image = conn.get_image(image_id)
# Get the flavor that we want to use by its id
flavor_id = '100'
flavor = conn.ex_get_size(flavor_id)
# Create the node with the name “PracticeInstance”
# and the size and image we chose above
instance = conn.create_node(name='PracticeInstance', image=image, size=flavor)
When I run the above code I am getting below error:
C:\Python dev\website\music\openstack>python openstack.py
Traceback (most recent call last):
File "openstack.py", line 30, in <module>
image = conn.get_image(image_id)
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\compute\drivers\openstack.py", line 2028, in get_image
'/images/%s' % (image_id,)).object['image'])
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack.py", line 223, in request
raw=raw)
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\base.py", line 536, in request
action = self.morph_action_hook(action)
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack.py", line 290, in morph_action_hook
self._populate_hosts_and_request_paths()
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack.py", line 324, in _populate_hosts_and_request_paths
osa = osa.authenticate(**kwargs) # may throw InvalidCreds
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack_identity.py", line 855, in authenticate
return self._authenticate_2_0_with_password()
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack_identity.py", line 880, in _authenticate_2_0_with_password
return self._authenticate_2_0_with_body(reqbody)
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\openstack_identity.py", line 885, in _authenticate_2_0_with_body
method='POST')
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\base.py", line 637, in request
response = responseCls(**kwargs)
File "C:\Users\C5265680\AppData\Local\Programs\Python\Python36\lib\site-packages\libcloud\common\base.py", line 157, in __init__
message=self.parse_error())
libcloud.common.exceptions.BaseHTTPError: {"error": {"message": "The resource could not be found.", "code": 404, "title": "Not Found"}}
I have checked the logs in my server at /var/log/keystone and it does not give any error so I am guessing that I am able to login.
Also there are many examples which show that after the above steps I should be able to get list of images/flavors/servers.
Not sure why i am not able to connect. Can someone please help me with this.
I think you should modify the auth_url and provider arguments.
As following argument set is worked at my environment.
auth_username = 'admin'
auth_password = 'f882e2f4eaad434c'
TENANT_NAME = 'admin'
project_name = 'admin'
auth_url = 'http://192.168.56.101:5000'
region_name = 'RegionOne'
provider = get_driver(Provider.OPENSTACK)
conn = provider(auth_username,
auth_password,
ex_force_auth_url=auth_url,
ex_force_auth_version='2.0_password',
ex_tenant_name=project_name)
Updated 2017.11.16
#user8040338 Your error message is the first clue,
The resource could not be found. and status code 404, the most cause of its message and status code is wrong rest api url format.
Firstly, you need to check keystone v2.0 rest api format.
At the same time, you check again Libcloud reference.
For instance, the argument ex_force_auth_version had been specified the api version 2.0_password (v2), but auth_url variable formed the url including the version resource /v3, it was wrong version and usage of API with the libcloud API argument you specified. auth_url should be a base URL from API usage.
The similar processes about each argument of API should be conducted repeatedly until solving issues.
I want to change the userAccountControl and password of the AD user. User is already created in AD. The user is created with python-ldap module in AD and is in 'Disabled' state and with no password.
AD is hosted on win2k8R2.
When I change the uac and password with the pythion-ldap script it throws below error:
ldap://192.168.254.1:389
(97, [])
Traceback (most recent call last):
File "C:\workspace\utils\src\u.py", line 16, in <module>
l.modify_s(dn, mod_attrs)
File "C:\Python26\lib\site-packages\ldap\ldapobject.py", line 336, in modify_s
return self.result(msgid,all=1,timeout=self.timeout)
File "C:\Python26\lib\site-packages\ldap\ldapobject.py", line 436, in result
res_type,res_data,res_msgid = self.result2(msgid,all,timeout)
File "C:\Python26\lib\site-packages\ldap\ldapobject.py", line 440, in result2
res_type, res_data, res_msgid, srv_ctrls = self.result3(msgid,all,timeout)
File "C:\Python26\lib\site-packages\ldap\ldapobject.py", line 446, in result3
ldap_result = self._ldap_call(self._l.result3,msgid,all,timeout)
File "C:\Python26\lib\site-packages\ldap\ldapobject.py", line 96, in _ldap_call
result = func(*args,**kwargs)
ldap.UNWILLING_TO_PERFORM: {'info': '00002077: SvcErr: DSID-031903A4, problem 5003 (WILL_NOT_PERFORM), data 0\n', 'desc': 'Server is unwilling to perform'}
import ldap
host = "192.168.254.1"
ip = "ldap://%s:%d"%(host, 389)
l = ldap.initialize(ip)
newUser = "vishalworld"
dn = "cn=%s,%s"%(newUser, "cn=Users,DC=example,DC=com")
print l.simple_bind_s("administrator",password)
pwd = '"abcdefgh"'.encode("utf-16-le")
mod_attrs = [
(ldap.MOD_REPLACE, "lockoutTime", 0),
(ldap.MOD_REPLACE, "unicodePwd", pwd),
]
l.modify_s(dn, mod_attrs)
First you need to add tls support to your AD.http://araihan.wordpress.com/2009/10/05/windows-server-2008-active-directory-certificate-services-ad-cs/ this is a tutorial to explain how to create the certificate
Then you need to authenticate with tls to the AD. like this
import ldap
LDAP_SERVER_EMG = "ldaps://192.168.0.250"
BIND_DN = "Administrador#emgS.local"
BIND_PASS = "xxxXXXxxxXXXxxx"
USER_BASE = "dc=emgS,dc=local"
try:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
lcon_emg = ldap.initialize(LDAP_SERVER_EMG)
lcon_emg.simple_bind_s(BIND_DN, BIND_PASS)
except ldap.LDAPError, e:
print e
Then you can add your user for test
ad_u = {
'objectClass': ['top', 'person', 'organizationalPerson', 'user'],
'cn': 'test',
'displayName': 'test',
'distinguishedName': 'CN=test,DC=emgS,dc=local',
'givenName': 'test test',
'sAMAccountName': 'test',
'sn': 'test',
'userAccountControl': '514',
'userPrincipalName': 'test#emgS.local',
'mail': mail_user,
#732 is test in 3ll1t3
'employeeID': '732'
}
mods = ldap.modlist.addModlist(ad_u)
try:
lcon_emg.add_s(ad_u.get('distinguishedName'),
mods)
except Exception, e:
response.update({'error_ad': 'ActiveD: Error %s' % str(e)})
else:
response.update({'success_ad': 'ActiveD: F4ck y34h'})
#then you can put a password for the user
unicode_pass = unicode('\"' + "my super secret password :)" + '\"', 'iso-8859-1')
password_value = unicode_pass.encode('utf-16-le')
add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value])]
# 512 will set user account to enabled
mod_acct = [(ldap.MOD_REPLACE, 'userAccountControl', '512')]
try:
lcon_emg.modify_s(ad_u.get('distinguishedName'), add_pass)
except ldap.LDAPError, error_message:
response.update({'error_ad_clave': 'ActiveD: Error %s' % str(error_message)})
else:
response.update({'success_ad_clave': 'ActiveD: Yeah'})
try:
lcon_emg.modify_s(ad_u.get('distinguishedName'), mod_acct)
except ldap.LDAPError, error_message:
response.update({'error_ad_hab': 'Error %s' % str(error_message)})
else:
response.update({'success_ad_hab': 'Success'})
lcon_emg.unbind_s()
and TA TA!!!